4 messages in com.googlegroups.sqlalchemy[sqlalchemy] Confused by foreign_keys...| From | Sent On | Attachments |
|---|---|---|
| King Simon-NFHD78 | 28 Mar 2007 07:39 | |
| Michael Bayer | 28 Mar 2007 09:18 | |
| King Simon-NFHD78 | 28 Mar 2007 10:00 | .py |
| Michael Bayer | 28 Mar 2007 10:17 |
| Subject: | [sqlalchemy] Confused by foreign_keys argument![]() |
|---|---|
| From: | King Simon-NFHD78 (simo...@motorola.com) |
| Date: | 03/28/2007 07:39:09 AM |
| List: | com.googlegroups.sqlalchemy |
Hi,
I don't think I understand exactly what I'm supposed to pass to the foreign_keys parameter to relation.
I'm trying to set up a 1-to-many viewonly relation, with no backref, where there are two intermediate tables between the parent table and the child. To complicate things further, I need to restrict the rows by joining against a couple of other tables as well.
I was previously running on a fairly old version of SA (revision 2283), and the relation seemed to work correctly when I ANDed all of my conditions together and passed them to relation as the primaryjoin.
However, I just tried 0.3.6 and got "ArgumentError: Cant locate any foreign key columns ... Specify foreign_keys argument ..."
(Interestingly, if I try 0.3.5, the relation compiles but when I try to access it I get a different exception, InvalidRequestError, about a column not being available because it conflicts with another one)
For example, if these are my tables: #------------------------------------------------------------------ # Three entities, 'a', 'b' and 'c'. # 'a' and 'b' have a many-to-many relationship # 'b' and 'c' have a many-to-many relationship a_table = Table('a', metadata, Column('id', Integer, primary_key=True), Column('name', String(16)), )
b_table = Table('b', metadata, Column('id', Integer, primary_key=True), Column('name', String(16)), )
c_table = Table('c', metadata, Column('id', Integer, primary_key=True), Column('name', String(16)), )
a_b_table = Table('a_b', metadata, Column('a_id', Integer, ForeignKey('a.id'), primary_key=True), Column('b_id', Integer, ForeignKey('b.id'), primary_key=True), ) b_c_table = Table('b_c', metadata, Column('b_id', Integer, ForeignKey('b.id'), primary_key=True), Column('c_id', Integer, ForeignKey('c.id'), primary_key=True) )
class A(object): pass
class B(object): pass
class C(object): pass
mapper(B, b_table) mapper(C, c_table)
############################################################# # How can I create a mapper on A with a property that gives # all the 'C' objects? ############################################################# # This doesn't work - it requires the foreign_keys parameter # to be passed, but I don't know what to pass. mapper( A, a_table, properties={'cs': relation(primaryjoin=and_(a_table.c.id == a_b_table.c.a_id, a_b_table.c.b_id == b_c_table.c.b_id, c_table.c.id == b_c_table.c.c_id), viewonly=True, ) } )
#------------------------------------------------------------------
I probably haven't explained myself very well. I've tried lots of ways of expressing the relation, including passing a select as an association table, but I can't seem to get it to work. Does anyone have any ideas?
Thanks a lot,
Simon
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to sqla...@googlegroups.com
To unsubscribe from this group, send email to
sqla...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---





.py