Strapi Relations 101
Relationships are complicated. That’s true in real life. And that’s truer when managing content.
How many times weren’t you sure how to connect 2 pieces of content? To then realize too late that it wasn’t probably the best choice.
This post will give you key insights to make better decisions for your content modeling in Strapi.
Relations in Strapi
In Strapi, relations are a convenient way to connect different entities with each other. For reference, an entity is an instance of a content-type. So “My first post” is an entity of the Post collection type.
💡 An entity is an instance of a content-type.
You can connect content types using the relations field in the Content-Type Builder.
With flexibility in mind, Strapi offers an extensive set of options to build the best relationships for your content. However, this freedom comes with a price: a bit of confusion and doubts about what you should do.
To better understand how relations work let’s get first familiar with a few scary (read ‘technical’) words:
Another way to look at it is by asking yourself: “How many entities do you need to connect together?”
Strapi allows you all the possible combinations you can think of. Here they are with examples to illustrate them:
Example: a person has a unique id card.
Example: a post has several authors.
Example: A student can follow many courses. And a course is followed by many students.
Think of it as the flow of data: “Is it going one-way or both?”
You may be wondering why do we need to make a distinction. Both ways seem better right? In theory, yes. In practice, bidirectional relationships come with a significant performance cost for the database and hence the user experience. So you have to see where they make sense.
If you need to map thousands of cities for a country, the workload would be huge to retrieve all the cities when you are viewing a country’s details. In this case, you usually just need to connect a new city to an existing country.
A better use case for this type of directionality is being able to link a post to an author and vice versa. Since you probably won’t have thousands of entities for each content-type. If you end up in this situation, first of all, good for you! You have some prolific writers 🙂 But joke aside you might need to rethink your relations if you start to see some performances decrease.
This concept refers directly to the collection types you are using. Do you allow more than one to be connected?
This is the simplest case, you just want to connect 2 content-types, such as Authors to Posts.
This type is more complicated to handle on the database side. This is why it’s usually hard to pull off in a CMS.
A nice way to illustrate this one is to use images as the collection type you want to connect to multiple content-types, such as authors, posts, and more.
You are officially a pro in relations. You know understand all the subtitles dealing with relations and you can now pick the best configurations knowing all the tradeoffs to take into account.
Feel ready to dig deeper into the topic? Give a read to our documentationto see how you can apply your new learnings in Strapi today.
Status update: in (a) relationship(s)