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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.