Get Table Name By Table Class In Sqlalchemy

Independent on whether you use declarative extension or not, you can use the Runtime Inspection API:

def get_name(TableClass):
    from sqlalchemy import inspect
    mapper = inspect(TableClass)
    print mapper.tables[0].name

Please note that a class can have multiple tables mapped to it, for example when using Inheritance.


According To:

How to discover table properties from SQLAlchemy mapped object

I can use this:

print TableClass.__table__.name

print TableClass.__tablename__

works for me