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.

Reply via email to