On Apr 16, 2012, at 4:03 AM, Max S. wrote: > Hi, > > for an association table I specified a ORM mapped class that I wanted > to use as secondary attribute in relationship(). But I discovered, > that a Table or a callable can be used only. Because in my application > I do not use any explicite Table definitions, but ORM only I need to > write: > > foo = relationship(Foo, secondary=BarToFoo.__table__) > > I would expect to be able to write: > foo = relationship(Foo, secondary=BarToFoo) > > in this case I get an error: > C:\Python25\lib\site-packages\sqlalchemy\sql\util.py", line 262, in > join_condition > AttributeError: 'BarToFoo' object has no attribute 'foreign_keys' > > Is there any reason that ORM library does not support this kind of > usage? If so, would you please explain, if not is there any chance to > support it in future.
The reason is that we try to discourage using a table as "secondary" that is also a mapped class. The ORM does not keep track of the fact that both BarToFoo as well as Bar.foo are reading/writing from/to the same table. Therefore if you add some new BarToFoo objects and also add some rows to Bar.foo, you could get duplicate inserts, and similar things can happen along the deletion path. When I do use a table for "secondary" that is also mapped, I will always use "viewonly=True" at least to eliminate the possibility of conflicting reads, and even then the scheme is only a performance enhancement. The usual path to using an association object like BarToFoo in conjunction with a direct link from Bar->Foo is to use the association proxy, documented at http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html, which allows you to deal with the BarToFoo object in a transparent way. So anyway, the "secondary" requiring a Table directly is intended to highlight that "secondary" is usually meant to be the only source of ORM mapping to a particular Table. > > -- > 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. > -- 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.