簡體   English   中英

SQLAlchemy將一個映射的類中的多個外鍵映射到同一主鍵

[英]SQLAlchemy multiple foreign keys in one mapped class to the same primary key

我正在嘗試建立一個PostgreSQL表,該表具有兩個指向另一個表中相同主鍵的外鍵。

運行腳本時出現錯誤

sqlalchemy.exc.AmbiguousForeignKeysError:無法確定關系Company.stakeholder的父/子表之間的聯接條件-有多個鏈接表的外鍵路徑。 指定“ foreign_keys”參數,提供這些列的列表,這些列應被視為包含對父表的外鍵引用。

那是SQLAlchemy文檔中的確切錯誤,但是當我復制他們作為解決方案提供的內容時,該錯誤不會消失。 我可能做錯了什么?

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

我在這里看到了類似的問題,但一些問題的答案推薦一個使用primaryjoin但在文檔中就指出,你不需要primaryjoin在這種情況下。

試圖從foreign_keys中刪除引號並將它們制成列表。 來自有關Relationship Configuration: Handling Multiple Join Paths官方文檔Relationship Configuration: Handling Multiple Join Paths

在版本0.8中進行了更改: relationship()可以僅基於foreign_keys參數解決外鍵目標之間的歧義; 在這種情況下,不再需要primaryjoin參數。


下面的自包含代碼適用於sqlalchemy>=0.9

from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship, scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine(u'sqlite:///:memory:', echo=True)
session = scoped_session(sessionmaker(bind=engine))
Base = declarative_base()

#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])

Base.metadata.create_all(engine)

# simple query test
q1 = session.query(Company).all()
q2 = session.query(Stakeholder).all()

最新文檔:

文檔中foreign_keys=的形式會產生NameError,不確定在尚未創建類時預期如何工作。 通過一些黑客,我得以成功實現了這一點:

company_id = Column(Integer, ForeignKey('company.id'), nullable=False)
company = relationship("Company", foreign_keys='Stakeholder.company_id')

stakeholder_id = Column(Integer, ForeignKey('company.id'), nullable=False)
stakeholder = relationship("Company",
                            foreign_keys='Stakeholder.stakeholder_id')

換一種說法:

… foreign_keys='CurrentClass.thing_id')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM