烧瓶sqlalchemy多个外键关系(flask sqlalchemy multiple foreign keys in relationship)

编程入门 行业动态 更新时间:2024-10-26 19:31:25
烧瓶sqlalchemy多个外键关系(flask sqlalchemy multiple foreign keys in relationship)

嗨我正在使用flask和sqlalchemy,我正在尝试获得Team中的匹配关系以获得所有匹配,无论它是team1还是team2 (所以我想要的是能够通过匹配属性,无论它是Match1表中的team1还是team2 ),我得到错误:

sqlalchemy.exc.AmbiguousForeignKeysError:无法确定关系Team.matches上的父/子表之间的连接条件

链接表的链接路径有多个。 指定foreign_keys参数,提供应计为包含父表的外键引用的列的列表。

class Match(db.Model): id = db.Column(db.Integer, primary_key=True) map = db.Column(db.String(80), index=True) date = db.Column(db.DateTime, index=True) agreed = db.Column(db.Boolean) done = db.Column(db.Boolean) ladder_id = db.Column(db.Integer, db.ForeignKey('ladder.id')) team1_id = db.Column(db.Integer, db.ForeignKey('team.id')) team2_id = db.Column(db.Integer, db.ForeignKey('team.id')) team1_rounds = db.Column(db.Integer) team2_rounds = db.Column(db.Integer) team1_accepted_score = db.Column(db.Boolean) team2_accepted_score = db.Column(db.Boolean) points = db.Column(db.Integer) team1 = db.relationship('Team', foreign_keys='Match.team1_id') team2 = db.relationship('Team', foreign_keys='Match.team2_id') class Team(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), index=True) tag = db.Column(db.String(20), index=True, unique=True) captain_id = db.Column(db.Integer, db.ForeignKey('user.id')) captain = db.relationship('User', uselist=False, foreign_keys='Team.captain_id') members = db.relationship('User', backref='team', foreign_keys='User.team_id', lazy='dynamic') matches = db.relationship('Match', foreign_keys='[Match.team1_id, Match.team2_id]', lazy='dynamic')

Hi I'm using flask and sqlalchemy, I'm trying to get the matches relationship in Team to get all matches whether or not it is team1 or team2 (so what i want is to be able to get all matches for given team through the matches attribute regardless if it is team1 or team2 in the Match table), I get the error:

sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship Team.matches

There are multiple foreign key paths linking the tables. Specify the foreign_keys argument, providing a list of those columns which should be counted as containing a foreign key reference to the parent table.

class Match(db.Model): id = db.Column(db.Integer, primary_key=True) map = db.Column(db.String(80), index=True) date = db.Column(db.DateTime, index=True) agreed = db.Column(db.Boolean) done = db.Column(db.Boolean) ladder_id = db.Column(db.Integer, db.ForeignKey('ladder.id')) team1_id = db.Column(db.Integer, db.ForeignKey('team.id')) team2_id = db.Column(db.Integer, db.ForeignKey('team.id')) team1_rounds = db.Column(db.Integer) team2_rounds = db.Column(db.Integer) team1_accepted_score = db.Column(db.Boolean) team2_accepted_score = db.Column(db.Boolean) points = db.Column(db.Integer) team1 = db.relationship('Team', foreign_keys='Match.team1_id') team2 = db.relationship('Team', foreign_keys='Match.team2_id') class Team(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), index=True) tag = db.Column(db.String(20), index=True, unique=True) captain_id = db.Column(db.Integer, db.ForeignKey('user.id')) captain = db.relationship('User', uselist=False, foreign_keys='Team.captain_id') members = db.relationship('User', backref='team', foreign_keys='User.team_id', lazy='dynamic') matches = db.relationship('Match', foreign_keys='[Match.team1_id, Match.team2_id]', lazy='dynamic')

最满意答案

我从#sqlalchemy irc的那些人那里得到了帮助,所以我采用了错误的方法我现在建立了与主连接的关系:

matches = db.relationship('Match', primaryjoin="or_(Team.id==Match.team1_id, Team.id==Match.team2_id)", lazy='dynamic')

I got help from the guys on #sqlalchemy irc so i was going with the wrong approach I've now set up a relationship with a primaryjoin instead:

matches = db.relationship('Match', primaryjoin="or_(Team.id==Match.team1_id, Team.id==Match.team2_id)", lazy='dynamic')

更多推荐

本文发布于:2023-07-17 04:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139236.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:烧瓶   多个   关系   sqlalchemy   flask

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!