数据迁移:Strapi v3 和 v4 中的 SQL 关系
¥Data migration: SQL relations in Strapi v3 and v4
本文档是 数据迁移指南 中包含的 SQL 迁移指南 的一部分,旨在帮助你从 Strapi v3.6.x 迁移到 Strapi v4.0.x。
¥This documentation is part of the SQL migration guide included in the data migration guide and designed to help you migrate from Strapi v3.6.x to Strapi v4.0.x.
在 Strapi v3 中,只有多对多关系(双向和单向)和多态关系才会触发 SQL 连接表的创建。
¥In Strapi v3, only many-to-many relations (both bidirectional and unidirectional) and polymorphic relations trigger the creation of SQL join tables.
在 Strapi v4 中,2 个实体之间的每种类型的 relation 都会触发 SQL 连接表的创建。
¥In Strapi v4, every type of relation between 2 entities triggers the creation of a SQL join table.
本文档围绕使用 2 个不同数据库实体的示例构建:article
和 category
。该示例模拟向 article
实体添加指向 category
实体的关系。对于每种关系类型,以下部分说明:
¥This documentation is built around an example using 2 different database entities: article
and category
. The example simulates adding a relation to the article
entity pointing towards the category
entity. For each relation type, the following sections illustrate:
模型图式 中每个实体模型的属性定义
¥the definition of attributes in the model schema for each entity's model
以及实体关系图。
¥and the entity relationship diagrams.
Legend for entity relationship diagrams
实体关系图使用以下颜色代码:
¥Entity relationship diagrams use the following color code:
对于表和关系:
¥for tables and relations:
Strapi v3 项目为橙色
¥Strapi v3 items are in orange
Strapi v4 商品为紫色
¥Strapi v4 items are in purple
对于表名称(例如 articles_category_links):
¥for table names (e.g. articles_category_links):
实体名称(单数或复数)为蓝色
¥entity names (singular or plural) are in blue
模式中的属性名称为绿色
¥attribute names from a schema are in green
表名的所有其他部分均为黑色
¥all the other parts of a table name are in black
实体关系图还使用以下缩写:
¥Entity relationship diagrams also use the following abbreviations:
主键 PK
¥PK for primary key
FK 为外键
¥FK for foreign key
一对一关系 (1-1)
¥One-to-one relations (1-1)
模型架构(仅属性):
¥Model schemas (attributes only):
{
"category": {
"model": "category",
"via": "article"
}
}
{
"category": {
"type": "relation",
"relation": "oneToOne",
"target": "api::category.category",
"inversedBy": "article"
}
}
{
"article": {
"model": "article",
"via": "category"
}
}
{
"article": {
"type": "relation",
"relation": "oneToOne",
"target": "api::article.article",
"mappedBy": "category"
}
}
数据库结构:
¥Database structures:
一对多关系 (1-N)
¥One-to-many relations (1-N)
模型架构(仅属性):
¥Model schemas (attributes only):
{
"categories": {
"collection": "category",
"via": "article"
}
}
{
"categories": {
"type": "relation",
"relation": "oneToMany",
"target": "api::category.category",
"mappedBy": "article"
}
}
{
"article": {
"model": "article",
"via": "categories"
}
}
{
"article": {
"type": "relation",
"relation": "manyToOne",
"target": "api::article.article",
"inversedBy": "categories"
}
}
数据库结构:
¥Database structures:
多对一关系 (N-1)
¥Many-to-one relations (N-1)
模型架构(仅属性):
¥Model schemas (attributes only):
{
"category": {
"model": "category",
"via": "articles"
}
}
{
"category": {
"type": "relation",
"relation": "manyToOne",
"target": "api::category.category",
"inversedBy": "articles"
}
}
{
"articles": {
"collection": "article",
"via": "category"
}
}
{
"articles": {
"type": "relation",
"relation": "oneToMany",
"target": "api::article.article",
"mappedBy": "category"
}
}
数据库结构:
¥Database structures:
多对多关系 (N-N)
¥Many-to-many relations (N-N)
模型架构(仅属性):
¥Model schemas (attributes only):
{
"categories": {
"collection": "category",
"via": "articles",
"dominant": true
}
}
{
"categories": {
"type": "relation",
"relation": "manyToMany",
"target": "api::category.category",
"inversedBy": "articles"
}
}
{
"articles": {
"collection": "article",
"via": "categories"
}
}
{
"articles": {
"type": "relation",
"relation": "manyToMany",
"target": "api::article.article",
"mappedBy": "categories"
}
}
数据库结构:
¥Database structures:
单向关系(单向 N-1)
¥One-way relations (unidirectional N-1)
模型架构(仅属性):
¥Model schemas (attributes only):
{
"category": {
"model": "category"
}
}
{
"category": {
"type": "relation",
"relation": "oneToOne",
"target": "api::category.category"
}
}
数据库结构:
¥Database structures:
多向关系(单向 N-N)
¥Many-way relations (unidirectional N-N)
模型架构(仅属性):
¥Model schemas (attributes only):
{
"categories": {
"collection": "category"
}
}
{
"categories": {
"type": "relation",
"relation": "oneToMany",
"target": "api::category.category"
}
}
数据库结构:
¥Database structures:
多态关系
¥Polymorphic relations
在 Strapi v3 中,多态关系的表名称以 _morph
(单数)结尾,attribute_type
指向实体名称。
¥In Strapi v3, table names for polymorphic relations end with _morph
(singular), and the attribute_type
points to an entity name.
在 Strapi v4 中,多态关系的表名以 _morphs
(复数)结尾,并且 attribute_type
必须指向实体唯一标识符。
¥In Strapi v4, table names for polymorphic relations end with _morphs
(plural), and the attribute_type
must point to an entity unique identifier.
多态关系应始终在模式的属性中定义 “configurable”: false
,以防止在管理面板中对其进行修改。
¥Polymorphic relations should always have “configurable”: false
defined in the attributes of the schema to prevent their modification in the admin panel.
模型架构(仅属性):
¥Model schemas (attributes only):
{
"related": {
"collection": "*",
"filter": "field",
"configurable": false
}
}
{
"articles": {
"collection": "article",
"via": "related"
}
}
在 Strapi v3 中,仅为每个实体创建一个变形表。每当将多态关系属性添加到实体模式时,就会向 entity_morph
表添加 2 个新列:attribute_id
和 attribute_type
。
¥In Strapi v3, only one morph table is created for every entity. Whenever a polymorphic relation attribute is added to the entity schema, 2 new columns are added to the entity_morph
table: attribute_id
and attribute_type
.
{
"related": {
"type": "relation",
"relation": "morphToMany",
"configurable": false
}
}
{
"article": {
"type": "relation",
"relation": "morphMany",
"target": "article",
"morphBy": "related"
}
}
在 Strapi v4 中,为架构中定义的每个实体/变形关系创建一个变形表。如果单个实体有 2 个变形关系,则会创建 2 个不同的表,并使用以下格式命名:entity_attribute_morphs
。
¥In Strapi v4, a morph table is created for every entity/morph relation defined in the schema. If a single entity has 2 morph relations, 2 different tables are created and named using the following format: entity_attribute_morphs
.
数据库结构:
¥Database structures:
循环关系
¥Circular relations
循环关系是指向同一实体的关系(例如 article
→ article
)。在模式定义中,循环关系的定义方式与其他关系相同。
¥Circular relations are relations that point to the same entity (e.g. article
→ article
). In the schema definitions, circular relations are defined the same way as other relations.
在 Strapi v4 中,entity_id
和 inv_entity_id
用于区分初始条目与 SQL 连接表中相关条目的关系。
¥In Strapi v4, entity_id
and inv_entity_id
are used to differentiate the relation to the initial entry from the related entry in the SQL join table.
Strapi v4 中的数据库结构示例:
¥Database structures example in Strapi v4:
与组件的关系
¥Relations to components
Strapi v3 和 Strapi v4 中的组件架构定义相同,但数据库结构不同。
¥The schema definition for components is the same in Strapi v3 and in Strapi v4, but database structures differ.
模型架构(仅属性):
¥Model schemas (attributes only):
{
"component-name": {
"type": "component",
"component": "default.comp"
}
}
{
"component-name": {
"type": "component",
"component": "default.comp"
}
}
数据库结构:
¥Database structures: