On Feb 2, 2012, at 3:16 PM, Manpreet Bhurji wrote:

> I have a structure like this
> A.py
> class A:
>  _some_fields
> 
> B.py
> class B:
>  _field1id = Column(...)
>  _field1 = relationship( A.A, primaryjoin=lambda:A.A.id==B.field1id,
> backref="b", uselist=False )
>  _field2id = Column(...)
>  _field2 = relationship( A.A, primaryjoin=lambda:A.A.id==B.field2id,
> backref="b", uselist=False )
> 
> I know this will not work because the relationship field1 has already
> created "b" on A.
> I have tried using back_populates="b" in place of backref in class B
> and adding
> _b = relationship( "B", primaryjoin="or_(A.id==B.field1id,
> A.id==B.field2id)" ) to A
> 
> Any ideas on how I would go about this?

What object would A.b return if its "id" were present both in the "field1id" of 
one particular "B", and in the "field2id" of another ?    Would A.b be a 
collection of both ?  If so this requires a union of some kind, most easily 
done in Python.   Naming the backrefs "b_from_field1" and "b_from_field2", we'd 
say:

class A(Base):
    @property
    def b(self):
         return self.b_from_field1.union(self.b_from_field2)

it's also possible to create a relationship A.b that selects from both field1id 
and field2id by creating a UNION and then mapping B to that union using 
non_primary=True, then using that mapper as the target of A.b, though this is a 
little more involved.

In both cases A.b is read-only since it cant be determined if new B() entries 
would be established via the B._field1 relationship or the B._field2 
relationship.


-- 
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.

Reply via email to