In sqlalchemy Core, I can get the table and table name from a regular 
column by calling `c.table.name`:

    from sqlalchemy import table, column

    t = table('foo', column('bar'))

    assert hasattr(t.c.bar, 'table')
    print(t.c.bar.table.name)

However, If I label a column, the label no longer has a `table` attribute:

    lab = t.c.bar.label('baz')
    
    assert not hasattr(lab, 'table')

Is there any way to go from the label to the column, or from the label to 
the table?

I've found this one, but it seems a bit clunky:

    lab = t.c.bar.label('baz')

    c = list(lab.base_columns)[0]

    print(c.table.name)

Will label.base_columns always return a set of at least 1 item, or do I 
need to check?

When will label.base_columns return more than 1 item?

If I wanted something generic that handles both columns and labels, will 
this work safely:

    def get_table_name(column):
        return list(column.base_columns)[0].table.name

    assert get_table_name(t.c.bar) == 'foo'
    assert get_table_name(t.c.bar.label('baz')) == 'foo'

Thanks and best regards,

Matthew

-- 
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/5ee8c50a-4f0d-4d18-92e1-a355e1431a0an%40googlegroups.com.

Reply via email to