SqlAlchemy。无法添加两个外键。有多个链接表的外键路径

时间:2019-11-23 21:13:27

标签: python sqlalchemy

我是sqlalchemy的新手。我需要在子(学生)类中创建两个外键。
现在,我可以执行以下操作:

>>> student_one = Student(name='Sam')
>>> student_two = Student(name='Nick')
>>> group_one = Group(group_number='ST141', students=[student_one, student_two], senior_student=student_one)
>>> group_one.students
>>> group_one.senior_student

那是正确的。现在我想在senior_student_of表中有Student字段。我尝试了很多方法来做某事,但是无法添加。 这是我的代码:

class Group(Base):
    __tablename__ = 'groups'
    id = Column(Integer, primary_key=True)
    group_number = Column(String(10), nullable=True, default='')
    study_hours = Column(Integer, default=0, nullable=True)
    lab_studies = Column(Integer, default=0, nullable=True)
    pract_studies = Column(Integer, default=0, nullable=True)

    curator = relationship('Tutor', backref='tutors', lazy='dynamic')
    students = relationship('Student', backref='groups', lazy='dynamic')
    senior_student = relationship('Student', uselist=False, lazy='joined')

    def __repr__(self):
        return self.group_number


class Student(Base):
    __tablename__ = 'students'
    id = Column(Integer, primary_key=True)
    surname = Column(String(20), nullable=True, default='', )
    name = Column(String(20), nullable=True, default='')
    patronymic = Column(String(20), nullable=True, default='')
    absences = Column(Integer, nullable=True, default=0)
    undone_labs = Column(Integer, nullable=True, default=0)
    unready_labs = Column(Integer, nullable=True, default=0)

    group_id = Column(Integer, ForeignKey(Group.id))
    group = relationship(Group)

    # This don't work
    senior_student_of_id = Column(Integer, ForeignKey(Group.id))
    senior_student_of = relationship(Group)

总是出现错误:

sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship Group.students - there are multiple foreign key paths linking the tables.  Specify the 'f
oreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference to the parent table.

最后,我需要正确获取student_one.senior_student_of并在那里看到ST141
无法搜索正确的解决方案。谢谢你的帮助! :)

1 个答案:

答案 0 :(得分:0)

您可以这样做:

group = relationship(Group, foreign_keys=[group_id, ])
senior_student_of = relationship(Group, foreign_keys=[senior_student_of_id, ])

还要从“组”中删除关系定义。使用backref定义backref,如下所示:

    class Group(Base):
        __tablename__ = 'groups'
        id = Column(Integer, primary_key=True)
        group_number = Column(String(10), nullable=True, default='')
        study_hours = Column(Integer, default=0, nullable=True)
        lab_studies = Column(Integer, default=0, nullable=True)
        pract_studies = Column(Integer, default=0, nullable=True)

        def __repr__(self):
            return self.group_number


    class Student(Base):
        __tablename__ = 'students'
        id = Column(Integer, primary_key=True)
        surname = Column(String(20), nullable=True, default='', )
        name = Column(String(20), nullable=True, default='')
        patronymic = Column(String(20), nullable=True, default='')
        absences = Column(Integer, nullable=True, default=0)
        undone_labs = Column(Integer, nullable=True, default=0)
        unready_labs = Column(Integer, nullable=True, default=0)

        group_id = Column(Integer, ForeignKey(Group.id))
        group = relationship(Group, foreign_keys=[group_id, ], backref="students")

        senior_student_of_id = Column(Integer, ForeignKey(Group.id), backref="senior_student")
        senior_student_of = relationship(Group, foreign_keys=[senior_student_of_id, ])

尽管我从未使用过uselist = False,所以我不知道它是否有效。

我不确定这是否真的是您的意思:

students = relationship('Student', backref='groups', lazy='dynamic')

这会将backref组放入Student中,这意味着您将拥有Student.group s 。但您也可以在Student类中定义Student.group。