我在文档中找不到任何有关此信息的信息,但是如何获取SQLAlchemy中创建的表的列表?
我使用类方法来创建表。
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 tables.
最满意答案
所有表都收集在sqlalchemy MetaData对象的tables属性中。 只需获得这些表的名称列表:
>>> metadata.tables.keys() ['posts', 'comments', 'users']如果您使用声明式扩展名,那么您可能不是自己管理元数据。 幸运的是,元数据仍然存在于基础类上,
>>> Base = sqlalchemy.ext.declarative.declarative_base() >>> Base.metadata MetaData(None)如果你想弄清楚你的数据库中存在哪些表,即使是你还没有告诉sqlalchemy的那些,那么你可以使用表格反射。 然后,sqlalchemy将检查数据库,并使用所有缺少的表更新元数据。
>>> metadata.reflect(engine)All of the tables are collected in the tables attribute of the SQLAlchemy MetaData object. To get a list of the names of those tables:
>>> metadata.tables.keys() ['posts', 'comments', 'users']If you're using the declarative extension, then you probably aren't managing the metadata yourself. Fortunately, the metadata is still present on the baseclass,
>>> Base = sqlalchemy.ext.declarative.declarative_base() >>> Base.metadata MetaData(None)If you are trying to figure out what tables are present in your database, even among the ones you haven't even told SQLAlchemy about yet, then you can use table reflection. SQLAlchemy will then inspect the database and update the metadata with all of the missing tables.
>>> metadata.reflect(engine)更多推荐
发布评论