繁体   English   中英

如何将多个外键映射到 SQLAlchemy 中的同一个父级?

[英]How to map multiple foreign keys to the same parent in SQLAlchemy?

将第二个外键添加到同一个表后,我收到以下错误:

Please specify the 'onclause' of this join explicitly.

如何指定这种关系?

class Parent(Base):
    First = Column(Integer, ForeignKey('Child.Ex1'))
    Second = Column(Integer, ForeignKey('Child.Ex2'))

class Child(Base):
    Ex1 = Column(Integer)
    Ex2 = Column(Integer)

(编注: pep8建议命名以小写字母开头的类属性......只是一个约定)

class Parent(Base):
    __tablename__ = "Parent"
    id = Column(Integer, primary_key=True)
    first = Column("First", Integer, ForeignKey('Child.Ex1'))
    second = Column("Second", Integer, ForeignKey('Child.Ex2'))

    first_child = relationship("Child", primaryjoin="Parent.first==Child.ex1")
    second_child = relationship("Child", primaryjoin="Parent.second==Child.ex2")

class Child(Base):
    __tablename__ = "Child"
    id = Column(Integer, primary_key=True)
    ex1 = Column("Ex1", Integer)
    ex2 = Column("Ex2", Integer)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM