我可以在与SQLalchemy的同一表中指出两个外国钥匙吗?
因此,我有两个表:
# "parent" table, each user has multiple "reports_received" and "reports_made"
class User(Base):
__tablename__ = "users"
id: str = Column("id", String(16), primary_key=True)
reports_received = relationship("Report", back_populates="reported_user")
reports_made = relationship("Report", back_populates="reporting_user")
# "child" table, each report has one "reported_user" and "reporting_user"
class Report(Base):
__tablename__ = "reports"
report_id: int = Column("report_id", Integer(), primary_key=True)
reported_user_id = Column("reported_user_id", ForeignKey("users.id"))
reporting_user_id = Column("reporting_user_id", ForeignKey("users.id"))
reported_user = relationship("User",back_populates="reports_received", foreign_keys=[reported_user_id])
reporting_user = relationship("User", back_populates="reports_made", foreign_keys=[reporting_user_id])
报告应具有record_user
和reporting_user
,都存在于“用户”表中。 但这就是我尝试创建一些用户并分配一些报告时得到的:
sqlalchemy.exc.biguulforeignkeyserror:无法确定关系用户上的父/子表之间的加入条件。Reports_received-有多个链接表的外键路径。指定“ forefer_keys”参数,提供了这些列的列表,这些列应算作包含对父表的外键引用。
我不明白为什么会给我这个错误,因为我已经指定了外国键。我是否缺少其他事情,或者做这样的事情没有多大意义?
So I have two tables:
# "parent" table, each user has multiple "reports_received" and "reports_made"
class User(Base):
__tablename__ = "users"
id: str = Column("id", String(16), primary_key=True)
reports_received = relationship("Report", back_populates="reported_user")
reports_made = relationship("Report", back_populates="reporting_user")
# "child" table, each report has one "reported_user" and "reporting_user"
class Report(Base):
__tablename__ = "reports"
report_id: int = Column("report_id", Integer(), primary_key=True)
reported_user_id = Column("reported_user_id", ForeignKey("users.id"))
reporting_user_id = Column("reporting_user_id", ForeignKey("users.id"))
reported_user = relationship("User",back_populates="reports_received", foreign_keys=[reported_user_id])
reporting_user = relationship("User", back_populates="reports_made", foreign_keys=[reporting_user_id])
Reports should have a reported_user
and a reporting_user
, both being present in the "users" table.
But this is what I get when I try to create some users and assign them some reports:
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship User.reports_received - 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.
I can't understand why it's giving me this error, since I've already specified the foreign keys. Is there something else I'm missing or does doing something like this not make much sense?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Sqlalchemy只需要更多帮助就可以通过在用户类中指定
forefer_keys =
来调和关系。切换类声明的顺序并添加这些声明,就像让我们这样做一样
SQLAlchemy just needs a little more help to reconcile the relationships by specifying the
foreign_keys=
in the User class as well. Switching the order of the class declarations and adding those, as inlets us do this