|
| 1 | ++++ |
| 2 | +date = '2026-03-17T8:00:00+01:00' |
| 3 | +draft = true |
| 4 | +title = 'SQLAlchemy - Relationships: Many-To-Many Relationships' |
| 5 | +categories = ['Note'] |
| 6 | +tags = ['Python', 'Database'] |
| 7 | ++++ |
| 8 | + |
| 9 | +多对多类型,何其名称暗示的一样,当无法认定任何一方为 "一" 的时候使用。 |
| 10 | + |
| 11 | +## Many-To-Many Relationships |
| 12 | + |
| 13 | +在标准的 one-to-many 一对多关系中,"多" 的一方会有一个 foreign key 外键指向 "一" 的一方。 |
| 14 | +但是在尝试建立 Product 和 Country 之前的关系时,在产品中添加指向国家的外键不行,因为这样一个产品就只能来自一个国家。 |
| 15 | +在国家中添加外键指向产品也不行,这样一个国家就只能对应一种产品。 |
| 16 | + |
| 17 | +### How Many-To-Many Relationships Work |
| 18 | + |
| 19 | +为了实现多对多关系,需要两个一对多关系来表示这种复杂的关系。 |
| 20 | +由于无法在两个表之间建立直接的多对多关系,因此需要添加一个称为连接表 join table 的第三个表。 |
| 21 | +每一边都和 join table 维护一个一对多关系,这意味着连接表有两个外键,指向两个表。 |
| 22 | + |
| 23 | +例如:`products` 表的 id 和 `products_countries` 的 products_id 形成 1:N 关系,`countries` 表的 id 和 `cuontry_id` 形成 1:N 关系。 |
| 24 | +这里的 `products_countries` 表就是 join table 连接表,常见的命名规范就是使用构成关系的两个实体的名称。 |
| 25 | + |
| 26 | +如果一个产品在 3 个国家生产,那么 join table 连接表就会有 3 对实例。 |
| 27 | +从另一个方面来看,对于一个已经创建了 7 个产品的国家,将会有对应数量的条目。 |
| 28 | +其 country_id 被设置为该国家,每个条目都将其与其中一个产品关联起来。 |
| 29 | + |
| 30 | +### A Simple Many-To-Many Relationship Implementation |
| 31 | + |
| 32 | +管理所有这些连接表的外键关系听起来像是噩梦,但 SQLAlchemy 会完成大部分困难的工作。 |
| 33 | +实现这种关系的第一步是创建连接表,该表将被 SQLAlchemy 管理,因此所有通过 Declarative Base 提供的类型都是非必要的。 |
| 34 | +相反,该表可以通过 SQLAlchemy Core 提供的 `Table` 类型创建。 |
| 35 | +为了方便引用 Product 表和还未创建的 Country 表,join table 管理表应被添加到这些类型的上面。 |
| 36 | + |
| 37 | +```Python |
| 38 | +from sqlalchemy import Table, Column |
| 39 | + |
| 40 | +ProductCountry = Table( |
| 41 | + 'products_countries', |
| 42 | + Model.metadata, |
| 43 | + Column('product_id', ForeignKey('products.id'), primary_key=True, nullable=False), |
| 44 | + Column('country_id', ForeignKey('countries.id'), primary_key=True, nullable=False), |
| 45 | +) |
| 46 | +``` |
0 commit comments