简体   繁体   English

为什么将SQLAlchemy关联对象中的外键标记为主键?

[英]Why are the foreign keys in a SQLAlchemy Association Object marked as primary keys?

Below is the documentation from sqlalchemy . 以下是sqlalchemy的文档。

Notice how for left_id and right_id in the Association Class, they are first marked as ForeignKey and then primary_key = True 请注意,对于关联类中的left_id和right_id,如何将它们首先标记为ForeignKey,然后将primary_key = True

It makes sense to me that they should be foreign keys because logically they are foreign keys to the other two parent and child tables. 在我看来,它们应该是外键,因为从逻辑上讲,它们是其他两个父表和子表的外键。

So whats the purpose of having them be primary keys as well? 那么,让它们成为主键的目的又是什么呢?

Whats going on here? 这里发生了什么? Please explain. 请解释。

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")

This is not unique to SQLAlchemy. 这不是SQLAlchemy独有的。 This is how many-to-many relationships are designed, which is based on the principles of Relational Database design. 这是基于关系数据库设计原则设计many-to-many relationships

In a many-to-many relationship, there is a need for an additional table, also called association table, which maps entries from the first table with corresponding entries from the second table. 在多对多关系中,需要一个附加表,也称为关联表,该表将第一个表中的条目与第二个表中的对应条目进行映射。

When the association table is defined, we need some primary key to uniquely identify records in the association table. 定义关联表后,我们需要一些主键来唯一地标识关联表中的记录。 Having a primary key creates an index, which speeds up joining operations and the search for records. 使用主键可以创建索引,从而加快联接操作和记录搜索的速度。

So, why have all the Foreign keys as a part of the Primary for the association table? 那么,为什么所有外键都作为关联表的主键的一部分? This is to make sure there are no duplicate entries of record a of table A and record b of Table B . 这是为了确保有记录的不重复条目atable A和记录bTable B In other words, to ensure uniqueness in the relationship, thus, avoiding duplication of relationships. 换句话说,确保关系的唯一性,从而避免关系的重复。

Association table can be created without declaring the Foreign keys as Primary keys. 可以在不将外键声明为主键的情况下创建关联表。 But this is not advisable . 但这是不可取的 By doing so, the join operations become slow unless indexes are created explicitly. 这样,除非显式创建索引,否则联接操作将变慢。 And, there is a good chance of having duplicated records of a relationship between Table A and Table B 并且,很有可能重复了Table ATable B之间的关系记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM