假设我定义了以下 SQLAlchemy 类:
Base = declarative_base()
class Person(Base):
__tablename__ = 'person'
id = Column(Integer, primary_key=True)
computers = relationship('Computer', backref=backref('owner', lazy='dynamic'))
class Computer(Base):
__tablename__ = 'computer'
id = Column(Integer, primary_key=True)
ownerid = Column(Integer, ForeignKey('person.id'))
进一步假设我以这种方式访问了惰性查询对象:
relation = getattr(Computer, 'owner')
我如何确定是否relation
引用单个实例Person
(即在多对一关系中,如本例中),或者是否relation
引用实例集合(如在一对多关系中)?换句话说,如何确定动态 SQLAlchemy 关系对象的关系类型?