我无法形成表之间的关系,创建表时没有问题,但尝试从文件写入数据时出现错误:
sqlalchemy.ext.AmbigouslyForeignKeysError:无法确定关系 Point.start_point 上父/子表之间的联接条件 - 有多个外键路径链接表。指定“foreign_keys”参数,提供应被视为包含对父表的外键引用的列的列表。
`class Point(Base):
__tablename__ = 'point'
id_point: Mapped[intpk]
name_point: Mapped[str100] = mapped_column(unique=True)
cost: Mapped[int]
start_point: Mapped[list['Route']] = relationship(back_populates='point_start')
finish_point: Mapped[list['Route']] = relationship(back_populates='point_finish')
class Route(Base):
__tablename__ = 'route'
id_route: Mapped[intpk]
id_start_point: Mapped[int] = mapped_column(ForeignKey('point.id_point'))
id_finish_point: Mapped[int] = mapped_column(ForeignKey('point.id_point'))
distance: Mapped[int]
point_start: Mapped['Point'] = relationship(back_populates='start_point', foreign_keys='[id_start_point]')
point_finish: Mapped['Point'] = relationship(back_populates='finish_point', foreign_keys='[id_finish_point]')`
我尝试按照文档中的方式进行操作,但也没有帮助
`class Customer(Base):
__tablename__ = "customer"
id = mapped_column(Integer, primary_key=True)
name = mapped_column(String)
billing_address_id = mapped_column(Integer, ForeignKey("address.id"))
shipping_address_id = mapped_column(Integer, ForeignKey("address.id"))
billing_address = relationship("Address", foreign_keys=[billing_address_id])
shipping_address = relationship("Address", foreign_keys=[shipping_address_id])`
我认为这些关系应该有效。
class Point(Base):
__tablename__ = 'point'
id_point: Mapped[intpk]
# These are considered `separate` from the relationships on Route so you have to set the fks here too.
start_for_routes: Mapped[list['Route']] = relationship(back_populates='point_start', foreign_keys='[Route.id_start_point]')
finish_for_routes: Mapped[list['Route']] = relationship(back_populates='point_finish', foreign_keys='[Route.id_finish_point]')
class Route(Base):
__tablename__ = 'route'
id_route: Mapped[intpk]
id_start_point: Mapped[int] = mapped_column(ForeignKey('point.id_point'))
id_finish_point: Mapped[int] = mapped_column(ForeignKey('point.id_point'))
# You can pass in the column in class scope OR...
point_start: Mapped['Point'] = relationship(back_populates='start_for_routes', foreign_keys=id_start_point)
# You can use the delayed resolution here too but you have to start with at least a class.
point_finish: Mapped['Point'] = relationship(back_populates='finish_for_routes', foreign_keys='[Route.id_finish_point]')