Loading
Current section: Data Relationships 4 exercises
solution

One-to-Many Relationships in Prisma

Transcript

00:00 Let's head over to our schema prisma file and right down here. We're going to create our note model So we'll say model note and this is going to have all the typical fields even copilot can fill this in for us So we've got our ID just like we have for our user model

00:18 We have a title and content and then created at and updated out all of that pretty standard stuff And then we have our relationship. So let's talk about the relationship here. So we're going to call this relationship owner

00:32 so when you have a note model or a note object you can access the owner by saying note dot owner and So this is going to reference the user model So that is named the same up there and then we define our relationship now copilot did its best But this is actually not the full relationship that we want. So

00:51 The the syntax for this is the relation Attribute and then we provide these options for this attribute. So Yes, the relationship field that controls that relationship is the owner ID field and that is a string and that again is Going to be on the note model. We will make a change to the user model here in just a sec

01:13 but the relationship is controlled by the note and Then it's Referencing the ID field of the owner. So if we go up here if we were to change this to User ID then down here. This would also be user ID So those two need to be the same, of course I'm not going to change that because that wouldn't make any sense

01:32 So then finally we also want to have an on delete so that it cascades So if the user gets deleted we delete all their notes as well We don't want to have any notes that don't have owners. So that makes sense and on update the same story and then for

01:50 The the reason that we're getting these red underlines is because we need to establish that Relationship on the user side of our model as well Even though in the database the user doesn't even know about the note You know our Prisma schema here to generate our client properly We do need to have some changes to our note model or to our user model as well

02:10 So we're gonna need that. There we go And when I saved we have a VS code plugin that automatically Creates that relationship on the user side for some very unknown reason that I do not understand they capitalize the Property on the user model. And so what that actually would mean is

02:31 If we're in a JavaScript file, we're going to have our user model that we got from Prisma and we're going to say yeah, it'd be Prisma user dot find first and If we also selected the note and here we have our user And yeah, it'd be oh wait There we go. So then we'd say user dot notes

02:54 Yeah, or yeah, it's note. We want notes So what they autocompleted or or fixed for us is it be user dot note? And that would be an array of those note objects. I do not understand why they do it this way It doesn't make any sense. So we want it to be notes. And now this can be user dot notes

03:14 That makes so much more sense. So be careful out there friends make sure that the you don't just blindly accept what the AI or your VS code extension is going to do maybe in the future AI will do all of this stuff for us But for right now AI and VS code plugins are not

03:34 Something you just accept without review So quick review that this is it we're finished with this piece and so we've created our model ID title content created a updated app and then our owner relationship or our relationship with the user the field is going to be owner, so if I've got a await

03:57 Prisma notes Find first or find many or whatever. We'll have our note and then that will be note dot user So that's or sorry, that'd be owner That is controlled by that property. So this actually is just kind of describing the relationship and

04:17 Describing what property will be set on the note object. This is where we actually create a field in the database that will store the That primary key or that foreign key relationship So that is that now I think we also want to actually

04:39 Do or push this change to our database, so let's grab the command for that we'll say NPX Prisma DB push and Boom, we've got that updated if I click this little plus now and oh here, you know what we might need to restart Prisma Studio and for that change, there we go and

05:01 Now if I go to note then I can create a note if I want to so let's yeah Let's go ahead and do that Scooch this over add a record We're gonna say go ahead and make the ID for us We could actually customize that ourselves if we wanted to we can change it to whatever we want But I'm not going to do that we'll say the title is

05:22 koalas are the best and No, really koalas rock Thank You Cody It's kind of a funny note to have because the owner of this note is going to be Cody So there we go And that will autofill our owner ID and so we can save that change and boom now

05:44 We've got that and we can take a look at our database as well You could use the SQL lite 3 command or you can you just use the VS code extension and here? We'll see we've got that note in there as well. So Just

05:59 Creating these one-to-many relationships. You just have to remember that the relationship is controlled by the ownee rather than the owner and and then you learn the syntax for how to make that happen in in Prisma and then make sure to

06:17 That you're setting the properties to be the properties you want to access on the objects and that is one-to-many relationships in Prisma