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

2024-09-30 02:32:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: 模型id数据库定义sqlalchemyonaccountperson

热门问题