Empty morphMany relations return [] instead of null when populated
Page summary:In Strapi 5, populating an empty
morphManyrelation -- includingtype: 'media', multiple: truefields such as a gallery -- returns[]instead ofnull. Update client code that checksfield === nullto treat[]as the empty state.
In Strapi 5, empty morphMany relations and type: 'media', multiple: true fields (such as a gallery) now serialize as an empty array when populated, consistent with oneToMany and manyToMany relations. Previously, these fields returned null when no related entries existed.
This page is part of the breaking changes database and provides information about the breaking change and additional instructions to migrate from Strapi v4 to Strapi 5.
Breaking change description
In Strapi v4
Populating an empty morphMany relation or a multiple media field returned null:
{
"gallery": null
}
In Strapi 5
Populating an empty morphMany relation or a multiple media field returns an empty array:
{
"gallery": []
}
Migration
This section regroups useful notes and procedures about the introduced breaking change.
Notes
- This change applies to all
morphManyrelation types, includingtype: 'media', multiple: truefields (for example, gallery fields). - The change is unconditional and is not controlled by the
api.documents.strictRelationssetting. - See Document Service API: Populating fields and REST API: Population & Field Selection for related information on populating relations.
Manual procedure
Check client code, webhooks, and integrations that consume populated morphMany or multiple media fields.
- Search your codebase for code that branches on
field === nullor treatsnullas "no value" for populatedmorphManyor multiple media fields. - Replace
nullchecks with array checks, for example using!field?.length.
Before
if (entry.gallery === null) {
// handle empty gallery
}
After
if (!entry.gallery?.length) {
// handle empty gallery
}