SQL Alchemy

编程入门 行业动态 更新时间:2024-10-28 07:22:52
SQL Alchemy - 获取表的列表(SQLAlchemy - Getting a list of tables)

我在文档中找不到任何有关此信息的信息,但是如何获取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)

更多推荐

本文发布于:2023-08-06 04:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444631.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SQL   Alchemy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!