regarding "lists of columns", FOREIGN KEY constraints refer to sets of columns between local table and referred table, not just a single pair of columns. That a typical foreign key constraint might only link a single column in the local and referred tables is a special case. an application that is looking at foreign key constraints and the columns they represent necessarily needs to look at local and referred columns as variable length sets.
On Thu, Dec 31, 2020, at 4:05 AM, Matthew Graham wrote: > > Hi I am new to SQLAlchemy, > > I am trying to extract from reflected tables: > 1. the column name of the current table > 2. the referred table name and > 3. the column name of the referred table > now I can manage to do this using (and for sake only using first forein key) > ** metadata = sqlalchemy.MetaData() > metadata.reflect(engine) > for table in metadata.tables.values(): > print(table.name)** --- Current table name* *** l = list(table.foreign_keys) > if len(l) > 0: > print(l[0].column)** --- Referred table column name (but is > prepended with table name and a .) > **** for fk in table.foreign_key_constraints: > print(fk.column_keys[0])** --- current table column name but > ** print(fk.referred_table)** --- Referred table name > ** > There must surely be a cleaner way to do this preferably: > * not needing to convert table.foreign_keys to a list and then check list > length > * getting referred table column name without the foreign table prepended as > would rather not have to add in regex replacements > * get the current table column name without having to index with column > keys[0] > > Thanks you > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > To post example code, please provide an MCVE: Minimal, Complete, and > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > description. > --- > 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 view this discussion on the web visit > https://groups.google.com/d/msgid/sqlalchemy/093cb4b3-381a-4476-aff9-02471d61efa1n%40googlegroups.com > > <https://groups.google.com/d/msgid/sqlalchemy/093cb4b3-381a-4476-aff9-02471d61efa1n%40googlegroups.com?utm_medium=email&utm_source=footer>. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- 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 view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/dbe97d96-ff84-4f84-9cb8-e1b125ae42c6%40www.fastmail.com.
