How to tell if a InstrumentedAttribute is a relation in sqlalchemy?

泄露秘密 提交于 2021-02-10 12:06:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!