Ah, so, thanks. My logic was that I could specify which foreign_key to use for the relationship, which is basically a subset of primaryjoin condition, but in my opinion "cleaner". So, that wouldn't work? I must use always use primaryjoin?



I was looking at few paragraphs below, under Multiple Relationships against the same Parent/Child, which is basically my situation, and shows a mapper based solution.

http://www.sqlalchemy.org/docs/orm/relationships.html#multiple-relationships-against-the-same-parent-child



Thanks for help!

.oO V Oo.


On 11/11/2011 07:32 PM, Michael Bayer wrote:
On Nov 11, 2011, at 3:46 AM, Vlad K. wrote:

Hi,


I have two models, A and B. Model B contains two foreign keys into table A, because it is 
a "comparator" model that describes certain logical interaction between two A 
models. However, I want model B to contain a relationship to both so I can access them 
through the model B instance:


class ModelB(Base):
    __tablename__ = ...

    id_b = ... # primary
    some_comparison_data = ...

    main_model_id = Column(Integer, ForeignKey("models_a.id_a", ondelete="cascade", 
onupdate="cascade"))
    duplicate_model_id = Column(Integer, ForeignKey("models_a.id_a", ondelete="cascade", 
onupdate="cascade"))

    main_model = relationship("ModelA", foreign_keys=[ ??? ], lazy="joined")
    duplicate_model = relationship("ModelA", foreign_keys=[ ??? ], 
lazy="joined")



ModelA has no keys back to B, this is basically a "one-to-two" relationship 
from B to A.
I would classify this as two distinct "many to one" relationships from B to A.


Now as you can see I don't know what to specify for foreign keys.
You don't need to specify foreign_keys here as the Column objects on ModelB already have 
valid ForeignKey objects back to ModelA.   You do however need to specify 
"primaryjoin" for each relationship() here, as it is otherwise ambiguous how a 
join from ModelB to ModelA should proceed:

main_model = relationship("ModelA", 
primaryjoin="ModelA.id_a==ModelB.main_model_id")
duplicate_model = relationship("ModelA", 
primaryjoin="ModelA.id_a==ModelB.duplicate_model_id")


I tried with foreign_keys=[main_model_id]    and    
foreign_keys=[duplicate_model_id]   but it complains it couldn't determine join 
condition and that I should use primaryjoin. Evenso I wouldn't know how to use 
it because I don't know what to specify:    class variable? a string?   The 
examples in the docs are not for declarative...
I'm always looking to improve the documentation.  In this case, all examples in the 
"relationship" documentation now use declarative for most examples, including 
"primaryjoin":

http://www.sqlalchemy.org/docs/orm/relationships.html#specifying-alternate-join-conditions-to-relationship

It's also in the declarative extension docs:

http://www.sqlalchemy.org/docs/orm/extensions/declarative.html#configuring-relationships

Perhaps you were looking at older documentation ?



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