Column has a collection "foreign_keys", a collection since a column in a 
relational database can have any number of foreign key constraints applied to 
it.  If this collection is non-empty, then that Column is associated with a 
ForeignKey and thus a ForeignKeyConstraint (note that a ForeignKey is 
ultimately an element in a larger ForeignKeyConstraint object).

Not sure what you mean by the "name" - assuming you mean the name of the column 
it refers to, ForeignKey has an accessor ".column" that will give you the 
Column to which it refers to:

http://docs.sqlalchemy.org/en/latest/core/schema.html?highlight=foreignkey#sqlalchemy.schema.ForeignKey.column

That gives you the Column itself with a ".name" attribute.

If by "name" you mean the name of the constraint itself, like for the purposes 
of "ADD CONSTRAINT/DROP CONSTRAINT", the ForeignKey object of each Column is by 
default unnamed - in relational DDL, you don't need to give explicit names to 
FK constraints, the database will name these automatically.  So unless you give 
them a name, the name can't be determined on the Python side.  When reflecting 
a table, the ForeignKeyConstraint is reflected along with the name, if the 
database supports giving us the name, and the name should be associated with 
each associated ForeignKey object.

On Mar 25, 2012, at 7:29 PM, lars van gemerden wrote:

> Hello,
> 
> Does anyone know a way get the names of the foreign key columns of a
> table, if the table/class is unknown beforehand?
> 
> I tried with "Column.foreign_keys", which gives a set (why?) of
> "ForeignKey" objects but the "name" attribute of "ForeignKey"  is not
> automatically set.
> 
> Cheers, Lars
> 
> -- 
> 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.
> 

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