如何定义跨多个表联接的间接相关实体之间的SQLAlchemy关系?

时间:2018-11-03 22:29:08

标签: python sqlalchemy

假设我具有以下数据库模型:

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=?;

0 个答案:

没有答案