I have a class that has two relationships to the same type of objects.
One of the relationships will store objects of type "VR" and the other
objects with a type "CC". One object can only be in one of the lists
(relationships) at the same time:
This is the "container" class and its two relationships:
class Container(rdb.Model):
rdb.metadata(metadata)
rdb.tablename("containers")
id = Column("id", Integer, primary_key=True)
relation1 = relationship("MyObject",
uselist=True,
primaryjoin=lambda: and_((MyObject.id == MyObject.containerId),
(MyObject._type == "VR")),
cascade="all, delete, delete-orphan"
)
relation2 = relationship("MyObject",
uselist=True,
primaryjoin=lambda: and_((MyObject.id == MyObject.containerId),
(MyObject._type == "CC")),
cascade="all, delete, delete-orphan"
)
I don't think there's need to mention, but, MyObject.containerId is
a ForeignKey "pointing" to the Container.id.
I'd like to know if there's a way to create a backref so I will be
able to access the "container" through the "MyObject" class. The idea
would be having something like:
relation1 = relationship("MyObject",
uselist=True,
primaryjoin=lambda: and_((MyObject.id == MyObject.containerId),
(MyObject._type == "VR")),
cascade="all, delete, delete-orphan",
backref=backref('container', order_by=id)
)
relation2 = relationship("MyObject",
uselist=True,
primaryjoin=lambda: and_((MyObject.id == MyObject.containerId),
(MyObject._type == "CC")),
cascade="all, delete, delete-orphan",
backref=backref('container', order_by=id)
)
But of course, that fails because it's trying to add two ".container"
fields to the "MyObject" class.
I have also seen that you can define "joins" in the backref, but I
haven't been able to find examples about how to define it. And I am
still not very sure that that would allow me to have to backrefs with
the same name/identifier.
I just need to know if it's even possible having two backrefs with the
same name. Actually, a "you really got the whole concept wrong" may
help too (if that's the case) . If it's doable, does any of you know
where can I find examples of "advanced" backref usage? With primary
joins, secondary joins and all that juicy stuff...
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 [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.