【发布时间】:2014-04-16 20:17:27
【问题描述】:
我正在尝试设置一个 postgresql 表,该表具有两个指向另一个表中相同主键的外键。
当我运行脚本时出现错误
sqlalchemy.exc.AmbiguousForeignKeysError:无法确定关系 Company.stakeholder 上的父/子表之间的连接条件 - 有多个外键路径链接表。指定 'foreign_keys' 参数,提供应计为包含对父表的外键引用的列的列表。
这是SQLAlchemy Documentation 中的确切错误,但当我复制他们提供的解决方案时,错误并没有消失。我可能做错了什么?
#The business case here is that a company can be a stakeholder in another company.
class Company(Base):
__tablename__ = 'company'
id = Column(Integer, primary_key=True)
name = Column(String(50), nullable=False)
class Stakeholder(Base):
__tablename__ = 'stakeholder'
id = Column(Integer, primary_key=True)
company_id = Column(Integer, ForeignKey('company.id'), nullable=False)
stakeholder_id = Column(Integer, ForeignKey('company.id'), nullable=False)
company = relationship("Company", foreign_keys='company_id')
stakeholder = relationship("Company", foreign_keys='stakeholder_id')
我在这里看到过类似的问题,但some of the answers 建议使用primaryjoin,但在文档中它指出在这种情况下您不需要primaryjoin。
【问题讨论】:
标签: python postgresql sqlalchemy