我正在尝试定义两个关系是间接关系的表之间的关系(即通过另外两个表).
我正在寻找的结果可以使用此查询获取:
(db.session.query(Telnum)
.filter(Account.customer==customer)
.filter(Account.account_id == Subscription.account_id)
.filter(Telnum.sub_id == Subscription.id)
.order_by(Telnum.telnum)
.all()
)
Run Code Online (Sandbox Code Playgroud)
哪里customer是Customer对象.
我正在努力弄清楚如何定义这种关系,类似于Customer.invoices关系.我有一个想法是这样的:
telnums = db.relationship('Telnum',
primaryjoin="and_(Account.user_id==Customer.id, "
"Account.account_id == Subscription.account_id, "
"Telnum.sub_id == Subscription.id)",
backref='customer')
Run Code Online (Sandbox Code Playgroud)
正如这篇文章所显示的,这不起作用.它产生的错误信息是:
sqlalchemy.exc.ArgumentError: Could not locate any simple equality expressions involving locally mapped foreign key columns for primary join condition 'accounts.user_id = customers.id AND accounts.account_id = subscriptions.account_id AND pstn_numbers.sub_id = subscriptions.id' on relationship Customer.telnums. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or are annotated in …