SQLAlchemy - Getting a list of tables

前端 未结 9 1163
青春惊慌失措
青春惊慌失措 2020-12-08 03:40

I couldn\'t find any information about this in the documentation, but how can I get a list of tables created in SQLAlchemy?

I used the class method to create the tab

9条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 04:12

    I'm solving same problem and found this post. After some try run, I would suggest use below to list all tables: (mentioned by zerocog)

    metadata = MetaData()
    metadata.reflect(bind=engine)
    for table in metadata.sorted_tables:
        print(table)
    

    This is useful for direct table handling and I feel is recommended.

    And use below code to get table names:

    for table_name in engine.table_names():
        print(table_name)
    

    "metadata.tables" provides a Dict for table name and Table object. which would also be useful for quick query.

提交回复
热议问题