如何判断SQLAlchemy中的InstrumentedAttribute是否为关系?
给定一个 sqlalchemy 模型类
class Table(Base):
__table__name = 'table'
col1 = Column(...)
rel = relationship(...)
如果你查看 Table,你会发现 col1 和 rel 都是 InstrumentedAttribute
类型的。那我该怎么区分这两者呢?我试着查看它们的属性,但属性太多了……而且关于这个问题没有文档说明。
1 个回答
11
检查一下 Table.<column>.property
的类型,这个通常可以是 ColumnProperty 或 RelationshipProperty 的实例:
>>> type(Table.col1.property)
<class 'sqlalchemy.orm.properties.ColumnProperty'>
>>> type(Table.rel.property)
<class 'sqlalchemy.orm.relationships.RelationshipProperty'>