问题
Given a sqlalchemy model class
class Table(Base):
__table__name = 'table'
col1 = Column(...)
rel = relationship(...)
If you look at Table, both col1 and rel are of type InstrumentedAttribute
. How can I distinguish these two? I tried to look into their attributes. But there are too many...and there's no document about this matter.
回答1:
Check the type of Table.<column>.property which can commonly be either instance of ColumnProperty or RelationshipProperty:
>>> type(Table.col1.property)
<class 'sqlalchemy.orm.properties.ColumnProperty'>
>>> type(Table.rel.property)
<class 'sqlalchemy.orm.relationships.RelationshipProperty'>
来源:https://stackoverflow.com/questions/26572268/how-to-tell-if-a-instrumentedattribute-is-a-relation-in-sqlalchemy