Current section: Data Relationships 4 exercises

One-to-Many Relationships


00:00 Now we're going to create our note model. So the note model is kind of interesting because it has a relationship with the user. So a single user can own multiple notes. And so we can make another note over here, and this note is owned by the user, and so on. The interesting thing here, though,

00:18 is that we can't have a single note that's owned by multiple users. That's not allowed. So each user has their own note. And also what's interesting is in a one-to-many relationship like we have here, where one user can own many notes, you actually have the arrows actually backward.

00:36 And so instead of having the user pointing to the note, the note actually points to the user like this. So the user actually will not have any reference to the note in the database. As far as the database is concerned, the user doesn't even know about the note.

00:53 The note is the one that stores the information about that relationship. So the note is going to have a column for the owner ID, and then the user will just be there. In a Prisma schema, you do define a relationship in the model in the user.

01:11 So you'll say what field you want the notes to be on that user model. But as far as the database is concerned, the note is the only one that knows about this one-to-many relationship. And so that's what you're going to be working on is adding this one-to-many relationship

01:29 in our Prisma schema for these notes. And of course, you'll be also making the properties for the note itself as well. So have a good time with this, and we'll see you when you're done.