Work with Models and Relations with Prisma Schema

Xiaoru Li
InstructorXiaoru Li

Share this video with your friends

Send Tweet
Published 2 years ago
Updated a year ago

In this lesson, we will learn how to model data relationships with Prisma Schema.

We will be using a social network as our example, create data models such as users, posts, likes, comments, and model the relations between them. After this lesson, you should be able to model common relations like one-to-one (1:1), one-to-many (1:n), many-to-many (n:m) with Prisma.

At the time of recording this lesson, Prisma Migrate is in an experimental state, so the extra flag --experimental is needed to perform the save and up commands.

Xiaoru Li: [0:01] Let's go to the schema.prisma file and create a new User model. For our social network, each user is supposed to optionally have one profile, and each profile must have one and only one user. Let's create a one-to-one mapping between the two models.

[0:24] The question mark denotes an optional or nullable field. On one side of this relation, we must add an additional id field for referencing because in reality, it only makes sense to first create a user entry and then link a profile to it. We'll add a userId field in profile and add a relation attribute to the user field.

[0:48] Hitting Ctrl+Space in VS Code will give us some hints as of what we can put in here. The first thing we'll need is the fields on this profile model we want to use for the mapping. This is going to be the userId field that we just created.

[1:02] Then the second thing is a unique field on the other side of the relation, the user model. In our case, it's going to be id. Prisma will assign a name for this relation internally, but if we want to be more explicit, we can give it a custom name. If we do so, we'll need to specify this name on the user model as well.

[1:27] In a social network, a user can add other users as friends and also become one of the many friends of another user. What we need here is a self-referencing many-to-many relationship on the User model.

[1:40] Let's add two more fields, friends and friendOf, which are both going to be a list of users and are both of the relation UserFriendsUser with the reference set to the id field of the model.

[1:59] Users can publish posts on the social network, so let's also create a new model for the posts with a creation timestamp and add a list called posts on the User model.

[2:18] When we auto-format the schema, the Prisma VS Code extension will generate a one-to-many relation on the Post model. Let's rename the generated fields, make author required for a post or non-nullable, and assign a relation name to make it more clear.

[2:43] Now, since every user can like multiple posts and each post can be liked by multiple users, our next step is to model this many-to-many relation. First, let's create a field called likedPosts on the User model, which is going to be a list of posts with a relation name UserLikesPost.

[3:05] When we hit auto-format again, the VS Code extension will generate a one-to-many relation, which is not exactly what we want. Let's rename it, change it into a list, and remove the fields in the relation attribute.

[3:22] A social network wouldn't be social without a comment section. Let's create a new Comment model. Let's add a list called comments on the Post model with the relation name PostHasComment.

[3:43] When we auto-format again, a new one-to-many relation will be generated. Let's make the field name lowercase and required because a comment not attached to a post doesn't make sense. An additional feature we can have is to make the comments nested. Each comment is going to have optionally one parent and a list of child comments.

[4:12] Finally, each comment must have an author and can be liked by users. Similar to what we did with Post, we'll create a new field called comments in the User model with a relation UserAuthorsComment, and another field, likedComments, with a relation UserLikesComment.

[4:32] Now, we copy over the author and likedBy fields from the Post model into the Comment model and rename everything to the correct relations. We are finished with modeling the data with relations using the Prisma schema. Let's run npx prisma migrate save to save our changes into a migration.

[4:58] Going into the corresponding migration directory, we can see an array of SQL commands generated based on the schema we just wrote. Let's run npx prisma migrate up to execute the migration.

[5:17] Then run npx prisma generate to update the generated Prisma Client for our schema. You should always be extremely careful with database migrations if you are modifying a schema in production with live data in it.