0
我想連接兩個模型。SQLAlchemy ManyToOne關係問題
模型#1被稱爲「組」。它通過starter_id,main_id,dessert_id連接到幾個「團隊」。我想要一個名爲「小組」的字段,其中包含所有連接小組的列表。
class Group(db.Model):
__tablename__ = 'groups'
id = db.Column(db.Integer, primary_key=True)
starter_id = db.Column(db.Integer, db.ForeignKey('teams.id'))
main_id = db.Column(db.Integer, db.ForeignKey('teams.id'))
dessert_id = db.Column(db.Integer, db.ForeignKey('teams.id'))
#Relationships
teams = db.relationship('Team', back_populates="group", foreign_keys= \
[starter_1_id, starter_2_id, starter_3_id, main_1_id, main_2_id, main_3_id, \
dessert_1_id, dessert_2_id, dessert_3_id])
class Team(db.Model):
__tablename__ = 'teams'
id = db.Column(db.Integer, primary_key=True)
group_id = db.Column(db.Integer, db.ForeignKey('groups.id'))
# Relationships
group = db.relationship("Group", back_populates="teams", foreign_keys=[group_id])
我得到這個錯誤:
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition
between parent/child tables on relationship Group.teams - there are multiple
foreign key paths linking the tables. Specify the 'foreign_keys' argument,
providing a list of those columns which should be counted as containing a
foreign key reference to the parent table.
我顯然不知道如何解決這個錯誤;)
謝謝工作正常。你能給我一些提示什麼是lambda? – Jan
您需要'lambda',因爲您在定義關係時沒有值'Team',因爲'Group'和'Team'每個都依賴於他們的定義。 SQLAlchemy支持爲'relation'的某些參數傳遞一個函數來解決這樣的循環依賴。 – univerio