该关系通过 与检测列表相关联property
。所以从这个普通的香草映射开始:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class A(Base):
__tablename__ = "a"
id = Column(Integer, primary_key=True)
bs = relationship("B")
class B(Base):
__tablename__ = "b"
id = Column(Integer, primary_key=True)
a_id = Column(Integer, ForeignKey('a.id'))
在任何最新版本的 SQLAlchemy 中,您都可以像这样看到它:
print A.bs.property.mapper.class_
在 0.8 中有更多可用的 API,您可以这样做:
from sqlalchemy import inspect
print inspect(A.bs).mapper.class_
检查文档-> http://docs.sqlalchemy.org/en/rel_0_8/core/inspection.html
“mapper->class_”的文档-> http://docs.sqlalchemy.org/en/rel_0_8/orm/mapper_config.html#sqlalchemy.orm.mapper.Mapper.class_