On Aug 10, 2014, at 2:52 PM, alchemy1 <[email protected]> wrote:
> I see that in ForeignKey I can use either a string or reference, so
> ForeignKey(MyModel.id) or ForeignKey('my_model.id'). Any advantage to using
> one or the other? What's the recommended approach?
whichever one is more convenient to the style in which you are setting up table
metadata is fine. The advantage of the string is that the target table
doesn't have to exist at the point at which you define the ForeignKey, which
eliminates the need to use imports for tables set up in other modules.
>
> When using the string way, is it recommended to use the model name like
> 'MyModel.id' or table name like 'my_model.id'?
the strings here are intercepted by ForeignKey() which works at the level of
SQLAlchemy Core, that is, it doesn't know anything about mapped classes, just
table names that are present in the same MetaData collection. So table
names. The class names won't resolve right now (could be something we add
later though).
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.