假设我具有以下数据库模型:
Person:
id
Account:
id
person_id -> Person.id
AccountPhoto:
id
account_id -> Account.id
photo_id -> Photo.id
Photo:
id
如何在SQLAlchemy中定义Person.photos
关系?
我遇到的困难是,它需要以某种方式以适当的方式将所有表连接在一起,从而有效地运行查询:
SELECT * FROM Photo
JOIN AccountPhoto ON Photo.id = AccountPhoto.photo_id
JOIN Account ON Account.id = AccountPhoto.account_id
JOIN Person ON Person.id = Account.person_id
WHERE Person.id=?;