2 回答

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊
您必須在父類中配置關(guān)系:
class Company(Base):
__tablename__ = 'company'
id = Column(Integer, primary_key=True)
name = Column(String)
employees = relationship('Employee', lazy='joined') # <<< Add this line
然后你可以在沒有連接的情況下查詢它:
companies = session.query(Company).all()
print(companies[0].employees)
文檔:https : //docs.sqlalchemy.org/en/13/orm/loading_relationships.html

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊
你可以這樣做:
class Company(Base):
__tablename__ = 'company'
id = Column(Integer, primary_key=True)
name = Column(String)
employees = db.session.query(Company, Employee).filter(Company.id = self.id).all()
self.employee_list = ['{0} {1}'.format(c.first_name, c.last_name) for c in employees]
然后你可以訪問員工姓名 Company.employee_list[0]
添加回答
舉報(bào)