以下是sqlalchemy的文档。为什么SQLAlchemy关联对象中的外键标记为主键?
注意如何在关联类left_id和right_id,他们 第一标记为ForeignKey的,然后primary_key =真
这是有道理的,我认为他们应该是外键,因为逻辑上它们是外键的其他两张父母和孩子的桌子。
那么,让它们成为主键的目的是什么呢?
这是怎么回事?请解释。
class Association(Base):
__tablename__ = 'association'
left_id = Column(Integer, ForeignKey('left.id'), primary_key=True)
right_id = Column(Integer, ForeignKey('right.id'), primary_key=True)
extra_data = Column(String(50))
child = relationship("Child", back_populates="parents")
parent = relationship("Parent", back_populates="children")
class Parent(Base):
__tablename__ = 'left'
id = Column(Integer, primary_key=True)
children = relationship("Association", back_populates="parent")
class Child(Base):
__tablename__ = 'right'
id = Column(Integer, primary_key=True)
parents = relationship("Association", back_populates="child")