[sqlalchemy] Inserting in a container class that behaves as list (or set) from the contained class (not from the container)

2011-01-21 Thread Hector Blanco
Hello list!

I have a couple of classes. One of the behaves as the container of the other:

class ContainerOfSamples(declarativeBase):
__tablename__ = containers

_id = Column(id, Integer, primary_key=True)
_samples = relationship(Samples, cascade=all, delete, 
collection_class=set)

def setSamples(self, samples):
self._samples = samples

def getSamples(self):
return self._samples

def addSample(self, sample):
self._samples.add(sample)

id = sqlalchemy.orm.synonym('_id', descriptor=property(getId, setId))
samples = sqlalchemy.orm.synonym('_samples',
descriptor=property(getSamples, 
setSamples))


class Sample(declarativeBase):
__tablename__ = containers

_id = Column(id, Integer, primary_key=True)
_whatever = Column(whatever, String(20))
_containerId = Column(container_id, Integer, 
ForeignKey(containers.id))
_container = relationship(Container, uselist=False)

def __hash__(self):
return int(self.id)

def setContainer(self, container):
self._container = container

def getContainer(self):
return self._container

id = sqlalchemy.orm.synonym('_id', descriptor=property(getId, setId))
whatever = sqlalchemy.orm.synonym('_whatever',
descriptor=property(getWhatever, 
setWhatever))
container = sqlalchemy.orm.synonym('_container',
descriptor=property(getContainer, 
setContainer))

It's a relationship 1:N (one sample can be in 1 container, 1 container
can have N samples)... basically, a list...

If I have an instance of ContainerOfSamples and I want to add a
sample, I can do:

container = ContainerOfSamples()
sample = Sample()
container.addSample(sample)

And the sample is properly added, the relationships are all
initialized/created/set (however you want to call it) properly... the
containerId in the sample is the id of the  container instance...
perfect.

So now the question is: Is there a way of getting the same effect from
the Sample class? Something like:

sample = Sample()
container = ContainerOfSamples()
sample.container(container)

And then in the container instance the Sample sample would be
added to the container.samples set?

It doesn't seem to work... for some reason, if I try to do that, the
sample._containerId becames the id of the sample...

I don't know if playing with the backref would give me what I
want... I've made a few tries, but it doesn't seem to improve... Maybe
I have a misconception here :-(

Any hints, examples, link to examples... would be helpful and deeply
appreciated.  Thank you in advance!

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Inserting in a container class that behaves as list (or set) from the contained class (not from the container)

2011-01-21 Thread A.M.

On Jan 21, 2011, at 12:29 PM, Hector Blanco wrote:

 Hello list!
 
 I have a couple of classes. One of the behaves as the container of the other:
 
 class ContainerOfSamples(declarativeBase):
   __tablename__ = containers
 
   _id = Column(id, Integer, primary_key=True)
   _samples = relationship(Samples, cascade=all, delete, 
 collection_class=set)
 
   def setSamples(self, samples):
   self._samples = samples
 
   def getSamples(self):
   return self._samples
 
   def addSample(self, sample):
   self._samples.add(sample)
 
   id = sqlalchemy.orm.synonym('_id', descriptor=property(getId, setId))
   samples = sqlalchemy.orm.synonym('_samples',
   descriptor=property(getSamples, 
 setSamples))
 
 
 class Sample(declarativeBase):
   __tablename__ = containers
 
   _id = Column(id, Integer, primary_key=True)
   _whatever = Column(whatever, String(20))
   _containerId = Column(container_id, Integer, 
 ForeignKey(containers.id))
   _container = relationship(Container, uselist=False)
 
   def __hash__(self):
   return int(self.id)
 
   def setContainer(self, container):
   self._container = container
 
   def getContainer(self):
   return self._container
 
   id = sqlalchemy.orm.synonym('_id', descriptor=property(getId, setId))
   whatever = sqlalchemy.orm.synonym('_whatever',
   descriptor=property(getWhatever, 
 setWhatever))
   container = sqlalchemy.orm.synonym('_container',
   descriptor=property(getContainer, 
 setContainer))
 
 
 sample = Sample()
 container = ContainerOfSamples()
 sample.container(container)

I don't understand the need for the synonyms, but shouldn't this be as simple 
as sample.container = container? The relationship on sample is already 
defined... maybe you are confused because you think you need these getters and 
setters- in the above example, I don't see any need for them.

Cheers,
M

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Inserting in a container class that behaves as list (or set) from the contained class (not from the container)

2011-01-21 Thread Hector Blanco
Thank you for the quick reply.

 shouldn't this be as simple as sample.container = container

Yeah... I thought so too... And actually, the getter/setters (the
synonym or property) just do that... (and a check for the parameter
type):

class Sample(declarativeBase):
   # yadda, yadda, yadda ...
   def setContainer(self, container):
  if isinstance(container, Container):
 self._container = container
  else:
 raise TypeError(received a %s when expecting a
Container % type(container))

Anyway... if my idea is not wrong, I'll check if the error is
somewhere else. It's good to know that I'm going in the right
direction!

Thank you!

2011/1/21 A.M. age...@themactionfaction.com:

 On Jan 21, 2011, at 12:29 PM, Hector Blanco wrote:

 Hello list!

 I have a couple of classes. One of the behaves as the container of the other:

 class ContainerOfSamples(declarativeBase):
       __tablename__ = containers

       _id = Column(id, Integer, primary_key=True)
       _samples = relationship(Samples, cascade=all, delete, 
 collection_class=set)

       def setSamples(self, samples):
               self._samples = samples

       def getSamples(self):
               return self._samples

       def addSample(self, sample):
               self._samples.add(sample)

       id = sqlalchemy.orm.synonym('_id', descriptor=property(getId, setId))
       samples = sqlalchemy.orm.synonym('_samples',
                                       descriptor=property(getSamples, 
 setSamples))


 class Sample(declarativeBase):
       __tablename__ = containers

       _id = Column(id, Integer, primary_key=True)
       _whatever = Column(whatever, String(20))
       _containerId = Column(container_id, Integer, 
 ForeignKey(containers.id))
       _container = relationship(Container, uselist=False)

       def __hash__(self):
               return int(self.id)

       def setContainer(self, container):
               self._container = container

       def getContainer(self):
               return self._container

       id = sqlalchemy.orm.synonym('_id', descriptor=property(getId, setId))
       whatever = sqlalchemy.orm.synonym('_whatever',
                                       descriptor=property(getWhatever, 
 setWhatever))
       container = sqlalchemy.orm.synonym('_container',
                                       descriptor=property(getContainer, 
 setContainer))


 sample = Sample()
 container = ContainerOfSamples()
 sample.container(container)

 I don't understand the need for the synonyms, but shouldn't this be as simple 
 as sample.container = container? The relationship on sample is already 
 defined... maybe you are confused because you think you need these getters 
 and setters- in the above example, I don't see any need for them.

 Cheers,
 M

 --
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 To unsubscribe from this group, send email to 
 sqlalchemy+unsubscr...@googlegroups.com.
 For more options, visit this group at 
 http://groups.google.com/group/sqlalchemy?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.