How to discover table properties from SQLAlchemy mapped object How to discover table properties from SQLAlchemy mapped object sqlite sqlite

How to discover table properties from SQLAlchemy mapped object


Information you need you can get from Table object:

  • Ship.__table__.columns will provide you with columns information
  • Ship.__table__.foreign_keys will list foreign keys
  • Ship.__table__.constraints, Ship.__table__.indexes are other properties you might find useful


If declarative base is not used, the answer fails. The following should work no matter how it's initialized

For Class Object:

TableClass.sa_class_manager.mapper.mapped_table.name

For Instance Object:

tableObj.sa_instance_state.mapper.mapped_table.name