SQLAlchemy-尽管使用了'foreignkeys ='参数,但是AmbiguousForeignKeysError

时间:2019-04-15 12:47:20

标签: python sqlalchemy

我正在创建一个使用SQLAlchemy的应用程序,但请继续运行 出现以下错误:

AmbiguousForeignKeysError: Could not determine join condition between 
parent/child tables on relationship Team.home_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.

我已经应用了documentation(设置foreign_keys属性)中提供的解决方案,但这并没有解决问题,并且错误不断出现。

这两个模型之间发生错误: 这个想法是该数据库将包含一个表,其中包含来自不同足球比赛的结果,以及一个表,其中包含不同的球队。

class Team(Base):
    __tablename__ = 'teams'

    id = Column(Integer, primary_key=True)
    name = Column(String(length=250), unique=True)
    home_matches = relationship("Match", back_populates='home')
    away_matches = relationship("Match", back_populates='away')

    def __repr__(self) -> str:
        return self.name
class Match(Base):
    __tablename__ = 'matches'

    id = Column(Integer, primary_key=True)
    date = Column(Date, nullable=False)
    home_id = Column(Integer, ForeignKey('teams.id'))
    home = relationship(
        "Team", back_populates='home_matches', foreign_keys=[home_id]
    )
    away_id = Column(Integer, ForeignKey('teams.id'))
    away = relationship(
        "Team", back_populates='away_matches', foreign_keys=[away_id]
    )
    home_score = Column(Integer, nullable=False)
    away_score = Column(Integer, nullable=False)

有人知道我在做什么错吗?

0 个答案:

没有答案