Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 805
David S. Avatar asked Jan 10 '23 13:01

David S.


1 Answers

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'>
like image 166
plaes Avatar answered Jan 14 '23 00:01

plaes