Check out a free preview of the full API Design in Node.js, v4 course:
The "Product Model" Lesson is part of the full, API Design in Node.js, v4 course featured in this preview video. Here's what you'd learn in this lesson:

Scott creates the schema for the Product model. A relationship between the Product and User models is established using the Prisma @relation attribute.

Get Unlimited Access Now

Transcript from the "Product Model" Lesson

[00:00:00]
>> Let's keep it going. Okay, so already kinda designed the schema out, we talked about it a little bit, and there's a million ways we can go with it, none of them are right or wrong, but this is kinda the way that I designed it. But looking at the design, I feel like there needs to be a product model, because I might have multiple products that I wanna post updates about.

[00:00:27] So I think we need a product model, cuz I'm a user, I'm working on this product, I'm working on that product, they all have their own separate updates. So I feel like I need a product model to keep track of those different products that I'm trying to show updates for.

[00:00:38] So that's what we're gonna do, let's make our product model. So if we go back to our code here, I can say model Product, like this. And then product also has an ID, so I'm just gonna just steal from the user, not a big deal. Same thing, it also has a createdAt, I'm gonna take that too.

[00:01:04] And then products have a name, which are gonna be String. If I wanna put a limit on the characters of that, I can do something called db, If you ever use SQL, you'll know what this is,VarChar. And I can say something like, I don't know, whatever limit you want, 255, which is a very common number in SQL.

[00:01:25] It has to do with reaching the maximum number of an eight bit piece of thing in memory for indexing, but you put whatever you want if you want to. You don't actually have to put anything if you don't want to. So I got the name, and then I need a product to belong to a user.

[00:01:43] So I have to set up a relationship here. So the way that I'm gonna do that is, one, thing about the relationship. Is it one-to-one, one-to-many, many-to-many? Well, one user can have many products, but a product can't belong to more than one user. So it's a one-to-many relationship on the product side.

[00:01:58] So what I'm gonna say is, first, I need to define the field in which the user is referenced. So I'll call this belongsToId. And it's gonna be type String. And the reason I see that is because it's going to be the ID of the user and the user's ID is string, so I think of it the same type.

[00:02:21] If the user's ID was number, I would have to call this number, okay? And then to set the relationship up, I'm gonna make another one called belongsTo ,not the ID part, and I can say this is gonna be type User like that. So this is type User. And then for some attributes here, I can say relation.

[00:02:44] And I can say fields, well, what fields in this product schema holds the relation? It's the belongsToId. And then it's like, okay, cool, and then what fields on the user does the belongToId reference? So I can say references. It references the ID field on the user. So just show you that again, I can say belongs, To User type relation.

[00:03:19] I want the belongsToId to be the field on a product that holds the relationship, and I want that to refer to the ID on the user. And you'll see that still get this squiggly, like something's still wrong. Yeah, we had to set the relationship on the other side as well.

[00:03:33] So we could do that manually, or if you just go to your terminal, you can just type out npx prisma format, like this, and it'll fix it for you. And what that does is, wait, I didn't save mine, that sucks. Let me override it right quick. Let me try that again.

[00:03:59] There we go. And what that does is you can see that it added the product onto the user for us as a mini, cuz it's an array, it's an array of products. I don't wanna call it product with a capital P, so I'm just gonna say products. I'm gonna pluralize it, so that's products.

[00:04:17] I can change the name of it there on the left if I don't like it, but this still has to be product with an array.
>> Could you explain the difference between ID and unique?
>> Yes, so the difference between ID and unique is that you can have more than one.

[00:04:38] IDs are also unique. And you can have more than one unique field on a model, but you can't have more than one ID. And IDs, you could think of it as the primary, I guess you could think about it like, I don't know, there might be a lot of unique things about you.

[00:04:58] But the thing that's guaranteed to always be unique globally is gonna be your Social Security number, right? So ID is just basically saying, this is the primary unique thing for this instance. And that's special because a database is doing optimizations on how it stores indexes, and it's going to prioritize things like IDs over other things for performance reasons.

[00:05:23] So it's just an indicator to let the database know that I need you to prioritize this when it comes to querying. It's a performance thing.
>> Are we able to make two references to the same model from another one?
>> Two references to the same model? Yeah, you could totally make two references to the same model as long as the other reference exist on the other side as well.

[00:05:43] So yeah, there's nothing stopping you. They just obviously can't be named the same.