hey there -

no need to deal with MetaData, Table, etc. just to get information about 
tables.  Have a look at the inspector interface and get the information 
directly:

https://docs.sqlalchemy.org/en/13/core/reflection.html#fine-grained-reflection-with-inspector

Table, MetaData etc. objects are only useful when you want to use them to 
compose SQL queries.



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 sqlalchemy+unsubscr...@googlegroups.com.
> 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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/52e78fb4-0bd2-4219-94a7-ce9cdb35792c%40www.fastmail.com.

Reply via email to