【问题标题】:How to tell if a InstrumentedAttribute is a relation in sqlalchemy?如何判断 InstrumentedAttribute 是否是 sqlalchemy 中的关系?
【发布时间】:2014-10-26 19:23:56
【问题描述】:

给定一个 sqlalchemy 模型类

class Table(Base):
  __table__name = 'table'
  col1 = Column(...)

  rel = relationship(...)

如果您查看 Tablecol1rel 都是 InstrumentedAttribute 类型。我怎样才能区分这两个?我试图调查他们的属性。但是太多了……没有关于这件事的文件。

【问题讨论】:

    标签: python sqlalchemy


    【解决方案1】:

    检查Table.<column>.property 的类型,通常可以是ColumnProperty 或RelationshipProperty 的实例:

    >>> type(Table.col1.property)
    <class 'sqlalchemy.orm.properties.ColumnProperty'>
    >>> type(Table.rel.property)
    <class 'sqlalchemy.orm.relationships.RelationshipProperty'>
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      相关资源
      最近更新 更多