Skip to main content

数据迁移:将 SQL 从 Strapi v3 迁移到 v4

¥Data migration:Migrate SQL from Strapi v3 to v4

Strapi 的数据库层已在 Strapi v4 中完全重写。本文档旨在通过比较 v3v4 表和列名称、数据结构和关系,重点介绍 Strapi v4 中引入的影响 SQL 数据库的重大更改。

¥The database layer of Strapi has been fully rewritten in Strapi v4. This documentation is designed to highlight the breaking changes introduced in Strapi v4 that impact SQL databases, by comparing v3 and v4 table and column names, data structures and relations.

更改可以是 global(影响任何表)或更有限的范围,仅影响 具体表格 或某些 Strapi 插件

¥Changes can be global (impacting any table) or more limited in scope, impacting only specific tables or some Strapi plugins.

🤓 Strapi v3 与 v4 中的关系

v3 与 v4 SQL 关系速查表 旨在帮助你了解 Strapi v3 和 Strapi v4 之间模型架构和实体关系图的差异。

¥The v3 vs. v4 SQL relations cheatsheet is designed to help you understand the differences in model schemas and entity relationship diagrams between Strapi v3 and Strapi v4.

⚠️ 🚧 迁移脚本

数据迁移脚本 目前正在进行 alpha/beta 测试。请随时在 专门的论坛主题 中分享反馈。

¥Data migration scripts are currently in alpha/beta testing. Please feel free to share feedback in the dedicated forum thread.

全局变化

¥Global changes

列大小写和时间戳列的更改可能会影响所有表。

¥Changes in column casing and timestamps columns can affect all tables.

列名大小写

¥Column name casing

🤓 v3/v4 比较

在 Strapi v3 中,列名称可以具有不同的大小写(例如 PascalCasecamelCasesnake_case)。

¥In Strapi v3 column names can have different casings (e.g. PascalCase, camelCase, and snake_case).

在 Strapi v4 中,每个列名称都应使用 snake_case

¥In Strapi v4, every column name should use snake_case.

要迁移到 Strapi v4,请确保所有列名称都使用 snake_case。模型模式中另一个外壳中定义的属性在与数据库层通信时,其名称将自动转换为 snake_case

¥To migrate to Strapi v4, make sure all column names use snake_case. Attributes defined in another casing in the model schema will have their name automatically transformed to snake_case when communicating with the database layer.

时间戳列

¥Timestamps columns

🤓 v3/v4 比较

时间戳列指的是 created_atupdated_at 列。

¥Timestamps columns refer to the created_at and updated_at columns.

在 Strapi v3 中,时间戳列直接由数据库层赋予默认值(即 CURRENT_TIMESTAMP)。

¥In Strapi v3, timestamps columns are given a default value (i.e. CURRENT_TIMESTAMP) directly by the database layer.

在 Strapi v4 中,无法重命名或禁用时间戳列。

¥In Strapi v4, timestamps columns can't be renamed or disabled.

要迁移到 Strapi v4,请将具有自定义列名称的时间戳迁移到 created_atupdated_at 字段,并从表结构中删除默认值。

¥To migrate to Strapi v4, migrate timestamps with custom column names to the created_at and updated_at fields, and remove the default value from the table structure.

影响 Strapi 内置表格的更改

¥Changes impacting Strapi built-in tables

Strapi 内置表格在 v3v4 中具有不同的名称,并且 Strapi v4 引入了新表格(由 ✨ 表情符号表示):

¥Strapi built-in tables have a different name in v3 and v4, and Strapi v4 introduces new tables (indicated by the ✨ emoji):

Strapi v3 中的表名称Strapi v4 中的表名称
strapi_permissionadmin_permissions(见 管理员权限
strapi_roleadmin_roles
strapi_administratoradmin_users
strapi_users_rolesadmin_users_roles_links
strapi_webhooksstrapi_webhooks
core_storestrapi_core_store_settings(见 核心店
(不适用)admin_permissions_role_link(见 管理员权限
(不适用)strapi_migrations
(不适用)strapi_api_tokens
(不适用)strapi_database_schema

管理员权限

¥Admin permissions

Strapi v3 中使用的 strapi_permission 表在 Strapi v4 中被命名为 admin_permissions,并且会进行以下其他更改:

¥The strapi_permission table used in Strapi v3 is named admin_permissions in Strapi v4, and is subject to the following other changes:

  • Strapi v3 和 Strapi v4 中的表结构不同:

    ¥The table structure is different in Strapi v3 and Strapi v4:

Strapi v3
Strapi v3 permissions
Strapi v4
Strapi v4 permissions
  • Strapi v4 中的角色关系在名为 admin_permissions_role_links 的连接表中处理。

    ¥The role relation in Strapi v4 is handled in a join table named admin_permissions_role_links.

  • 已为 Strapi v3 的 created_by_idupdated_by_id 列创建了新索引,名称如下:

    ¥New indexes have been created for the created_by_id and updated_by_id columns of Strapi v3, with the following names:

    Strapi v3 中的列名称Strapi v4 中的索引名称
    created_by_idadmin_permissions_created_by_id_fk
    updated_by_idadmin_permissions_updated_by_id_fk

核心店

¥Core store

Strapi v3 中使用的 core_store 表在 Strapi v4 中被命名为 strapi_core_store_settings

¥The core_store table used in Strapi v3 is named strapi_core_store_settings in Strapi v4.

核心存储表的结构保持不变,但模型定义和内容管理器配置已更改。

¥The structure of the core store table remains untouched, but model definitions and content manager configurations have changed.

模型定义

¥Model definitions

所有以 model_def_ 开头的行都已被删除,不再需要。

¥All the rows that begin with model_def_ have been dropped and are no longer required.

内容管理器配置

¥Content manager configurations

plugin_content_manager_configuration_content_types 开头的所有行均已更改,以匹配新的唯一标识符 (UID) 并反映 表名更改。这些更改包括 key 列的后缀和 value 列中的 uid 字段。

¥All the rows that begin with plugin_content_manager_configuration_content_types have been changed to match new unique identifiers (UIDs) and reflect table names changes. These changes include both the suffix of the key column and the uid field in the value column.

除了已重命名的所有内容类型(参见 表名更改)之外,以下 UID 也已更改:

¥In addition to all the content-types that have been renamed (see table names changes), the following UIDs have changed:

Strapi v3 中的 UIDStrapi v4 中的 UID
applicationapi
pluginsplugin

权限映射

¥Permissions mapping

下表列出了 Strapi v3 和 Strapi v4 之间所有权限的映射:

¥The following tables list the mapping of all permissions between Strapi v3 and Strapi v4:

Permission mapping for an example "restaurant" API:
Strapi v3 型Strapi v3 控制器Strapi v3 动作Strapi v4 动作
applicationrestaurantcount(已删除)
applicationrestaurantcreateapi::restaurant.restaurant.create
applicationrestaurantdeleteapi::restaurant.restaurant.delete
applicationrestaurantfindapi::restaurant.restaurant.find
applicationrestaurantfindoneapi::restaurant.restaurant.findOne
applicationrestaurantupdateapi::restaurant.restaurant.update
applicationrestaurantcreatelocalizationapi::restaurant.restaurant.createLocalization
Permission mapping for the Users & Permission plugin:
Strapi v3 型Strapi v3 控制器Strapi v3 动作Strapi v4 动作
users-permissionsauthcallback插件::users-permissions.auth.callback
users-permissionsauthconnect插件::users-permissions.auth.connect
users-permissionsauthemailconfirmation插件::users-permissions.auth.emailConfirmation
users-permissionsauthforgotpassword插件::users-permissions.auth.forgotPassword
users-permissionsauthregister插件::users-permissions.auth.register
users-permissionsauthresetpassword插件::users-permissions.auth.resetPassword
users-permissionsauthsendemailconfirmation插件::users-permissions.auth.sendEmailConfirmation
users-permissionsusercount插件::users-permissions.user.count
users-permissionsusercreate插件::users-permissions.user.create
users-permissionsuserdestroy插件::users-permissions.user.destroy
users-permissionsuserdestroyall(已删除)
users-permissionsuserfind插件::users-permissions.user.find
users-permissionsuserfindone插件::users-permissions.user.findOne
users-permissionsuserme插件::用户权限.user.me
users-permissionsuserupdate插件::用户权限.user.update
users-permissionsuserspermissionsgetrole插件::users-permissions.role.getRole
users-permissionsuserspermissionsgetroles插件::users-permissions.role.getRoles
users-permissionsuserspermissionscreaterole插件::users-permissions.role.createRole
users-permissionsuserspermissionsdeleterole插件::users-permissions.role.deleteRole
users-permissionsuserspermissionsupdaterole插件::users-permissions.role.updateRole
users-permissionsuserspermissionsgetpermissions插件::users-permissions.permissions.getPermissions
users-permissionsuserspermissionsgetadvancedsettings(已删除)
users-permissionsuserspermissionscustomroute(已删除)
users-permissionsuserspermissionsgetemailtemplate(已删除)
users-permissionsuserspermissionsgetpolicies(已删除)
users-permissionsuserspermissionsgetproviders(已删除)
users-permissionsuserspermissionsgetroutes(已删除)
users-permissionsuserspermissionsindex(已删除)
users-permissionsuserspermissionssearchusers(已删除)
users-permissionsuserspermissionsupdateadvancedsettings(已删除)
users-permissionsuserspermissionsupdateemailtemplate(已删除)
users-permissionsuserspermissionsupdateproviders(已删除)
Permission mapping for the i18n plugin:
Strapi v3 型Strapi v3 控制器Strapi v3 动作Strapi v4 动作
国际化content-typesgetnonlocalizedattributes(已删除)
国际化等区域设置listisolocales(已删除)
国际化localescreatelocale(已删除)
国际化localesdeletelocale(已删除)
国际化localeslistlocales插件::i18n.locales.listLocales
国际化localesupdatelocale(已删除)
Permission mapping for the Content-Type Builder:
Strapi v3 型Strapi v3 控制器Strapi v3 动作Strapi v4 动作
内容类型构建器buildergetreservednames(已删除)
内容类型构建器componentcategoriesdeletecategory(已删除)
内容类型构建器componentcategorieseditcategory(已删除)
内容类型构建器componentscreatecomponent(已删除)
内容类型构建器componentsdeletecomponent(已删除)
内容类型构建器componentsgetcomponent插件::content-type-builder.components.getComponent
内容类型构建器componentsgetcomponents插件::content-type-builder.components.getComponents
内容类型构建器componentsupdatecomponent(已删除)
内容类型构建器connectionsgetconnections(已删除)
内容类型构建器contenttypescreatecontenttype(已删除)
内容类型构建器contenttypesdeletecontenttype(已删除)
内容类型构建器contenttypesgetcontenttype插件::content-type-builder.content-types.getContentType
内容类型构建器contenttypesgetcontenttypes插件::content-type-builder.content-types.getContentTypes
内容类型构建器contenttypesupdatecontenttype(已删除)
Permission mapping for the Upload plugin:
Strapi v3 型Strapi v3 控制器Strapi v3 动作Strapi v4 动作
uploaduploadcount插件::upload.content-api.count
uploaduploaddestroy插件::upload.content-api.destroy
uploaduploadfind插件::upload.content-api.find
uploaduploadfindone插件::upload.content-api.findOne
uploaduploadgetsettings(已删除)
uploaduploadsearch(已删除)
uploaduploadupdatesettings(已删除)
uploaduploadupload插件::upload.content-api.upload
Permission mapping for the Email plugin:
Strapi v3 型Strapi v3 控制器Strapi v3 动作Strapi v4 动作
emailemailgetsettings(已删除)
emailemailsend插件::email.email.send
emailemailtest(已删除)
Permission mapping for the Content Manager and the Documentation plugin:

所有权限都被删除。

¥All permissions were deleted.

影响 Strapi 插件的变化

¥Changes impacting Strapi plugins

Strapi v4 引入了重大更改,这些更改影响 用户和权限上传国际化(i18n) 插件使用的表名称、列名称和数据库结构。

¥Strapi v4 introduces breaking changes that impact the table names, column names and database structures used by the Users & Permissions, Upload and Internationalization (i18n) plugins.

用户和权限插件

¥Users and Permissions plugin

用户和权限插件 使用的表和数据库结构在 Strapi v3 和 Strapi v4 中有所不同:

¥The tables and database structure used by the Users & Permissions plugin are different in Strapi v3 and Strapi v4:

Strapi v3:v3

Strapi v4:v4

启用的权限

¥Enabled permissions

在 Strapi v3 中,权限始终存在于表中,并且针对每个角色都是重复的。如果权限 "A" 的 enabled 列值设置为 1 (true),则为角色 "X" 设置权限 "A"。

¥In Strapi v3, permissions are always present in the table and are duplicated for each role. A permission "A" is set for a role "X" if permission "A" has the enabled column value set to 1 (true).

在 Strapi v4 中,数据库表中不存在未为任何角色启用的权限(即不再有 enabled 列)。如果存在以下情况,则为角色 "X" 设置权限 "A":

¥In Strapi v4, permissions that aren’t enabled for any role are not present in the database table (i.e. there is no more enabled column). A permission "A" is set for a role "X" if there is:

  • 表行中的权限 "A"

    ¥a permission "A" in the table rows

  • 以及连接表中将角色 "X" 链接到权限 "A" 的行。

    ¥and a row linking the role "X" to the permission "A" in the join table.

权限列

¥Permissions columns

在 Strapi v3 中,权限由 3 列定义:typecontrolleraction

¥In Strapi v3, permissions are defined by 3 columns:type, controller, and action.

在 Strapi v4 中,typecontrolleraction 列被名为 action 的单个列取代,聚合如下:action = transform(type).controller.action

¥In Strapi v4, the type, controller and action columns are replaced by a single column named action, aggregated like the following:action = transform(type).controller.action.

💡 提示

有关更多信息和具体示例,你可以比较 Strapi v3 users-permissions_permission 和 Strapi v4 up_permissions 表中相同权限的输出。

¥For more information and specific examples, you can compare the output of the same permission in Strapi v3 users-permissions_permission and Strapi v4 up_permissions tables.

上传插件

¥Upload plugin

在 Strapi v3 中,与文件内容类型关联的多态表被命名为 upload_file_morph,并且具有 idupload_file_id 属性。

¥In Strapi v3, the polymorphic table associated to the file content-type is named upload_file_morph and has both an id and an upload_file_id attribute.

在 Strapi v4 中,上传插件 的数据结构发生了以下变化:

¥In Strapi v4, the data structure of the Upload plugin is subject to the following changes:

  • 该多态性表被命名为 files_related_morphs。该名称包括 related,因为它涉及模型架构中定义的文件的 related 属性。

    ¥The polymorphic table is named files_related_morphs. The name includes related since it concerns the file’s related attribute defined in the model schema.

  • idupload_file_id 列不存在。

    ¥The id and upload_file_id columns do not exist.

  • 添加新列 file_id,作为指向 files.id 的外键。

    ¥A new column file_id is added, as a foreign key pointing to files.id.

  • file_id 列创建索引,作为 files_related_morph_fk

    ¥An index is created for the file_id column, as files_related_morph_fk.

国际化 (i18n) 插件

¥Internationalization (i18n) plugin

在 Strapi v4 中,国际化(i18n) 插件使用的本地化表遵循 循环多对多关系迁移,并从 entities__localizations 重命名为 entities_localizations_links

¥In Strapi v4, localization tables used by the Internationalization (i18n) plugin follow the circular many-to-many relationships migration and are renamed from entities__localizations to entities_localizations_links.

Strapi v3
Entity relationship diagram for i18n localizations in v3
Strapi v4
Entity relationship diagram for i18n localizations in v4