Question

Lors de l'ajout d'une seconde clé étrangère à la même table que je reçois l'erreur suivante:

Please specify the 'onclause' of this join explicitly.

Comment puis-je préciser cette relation?

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

class Child(Base):
    Ex1 = Column(Integer)
    Ex2 = Column(Integer)
Était-ce utile?

La solution

(ndlr:. pep8 recommande de nommer des attributs de classe en commençant par les minuscules ... juste une convention)

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)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top