How to discover table properties from SQLAlchemy mapped object(如何从 SQLAlchemy 映射对象中发现表属性)
问题描述
我有一个用表映射的类,在我的例子中是声明式的,我想从这个类中发现"表属性、列、名称、关系:
I have a class mapped with a table, in my case in a declarative way, and I want to "discover" table properties, columns, names, relations, from this class:
engine = create_engine('sqlite:///' + databasePath, echo=True)
# setting up root class for declarative declaration
Base = declarative_base(bind=engine)
class Ship(Base):
__tablename__ = 'ships'
id = Column(Integer, primary_key=True)
name = Column(String(255))
def __init__(self, name):
self.name = name
def __repr__(self):
return "<Ship('%s')>" % (self.name)
所以现在我的目标是从Ship"类从另一段代码中获取表列及其属性.我想我可以使用检测来处理它,但是 SQLAlchemy API 是否提供了任何方法?
So now my goal is from the "Ship" class to get the table columns and their properties from another piece of code. I guess I can deal with it using instrumentation but is there any way provided by the SQLAlchemy API?
推荐答案
您需要的信息可以从 表 对象:
Information you need you can get from Table object:
Ship.__table__.columns
将为您提供栏目信息Ship.__table__.foreign_keys
将列出外键Ship.__table__.constraints
、Ship.__table__.indexes
是您可能会觉得有用的其他属性
Ship.__table__.columns
will provide you with columns informationShip.__table__.foreign_keys
will list foreign keysShip.__table__.constraints
,Ship.__table__.indexes
are other properties you might find useful
这篇关于如何从 SQLAlchemy 映射对象中发现表属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 SQLAlchemy 映射对象中发现表属性


- 更改自动增量起始编号? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- SQL 临时表问题 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01