2016-05-11 94 views
3

嗨,我是用瓶和SQLAlchemy的,我試圖讓球隊的比賽關係到讓所有的比賽是否是TEAM1TEAM2(所以我要的是能夠得到所有匹配給定隊通過比賽,無論屬性,如果它是TEAM1TEAM2在Match),我得到的錯誤:瓶的SQLAlchemy關係中的多個外鍵

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') 

回答

5

我的幫助從#sqlalchemy IRC的球員,所以我用了錯誤的方法,我現在已經設置了一個primaryjoin替代關係會:

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