Data migration: SQL relations in Strapi v3 and v4
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.
In Strapi v3, only many-to-many relations (both bidirectional and unidirectional) and polymorphic relations trigger the creation of SQL join tables.
In Strapi v4, every type of relation between 2 entities triggers the creation of a SQL join table.
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
- for tables and relations:
- Strapi v3 items are in orange
- Strapi v4 items are in purple
- 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 for primary key
- FK for foreign key
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:
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:
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:
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:
One-way relations (unidirectional N-1)β
Model schemas (attributes only):
{
"category": {
"model": "category"
}
}
{
"category": {
"type": "relation",
"relation": "oneToOne",
"target": "api::category.category"
}
}
Database structures:
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β
In Strapi v3, table names for polymorphic relations end with _morph
(singular), and the attribute_type
points to an entity name.
In Strapi v4, table names for polymorphic relations end with _morphs
(plural), and the attribute_type
must point to an entity unique identifier.
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"
}
}
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"
}
}
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β
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.
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.
Database structures example in Strapi v4:
Relations to componentsβ
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: