Skip to main content

πŸ“ Creating Models

In creating schema we design the user schema. Now we design the product schema.
Its one to many relationship between user and Products.User can have multiple products.

  • id : Unique identifier for the product.
  • createdAt : The date and time when the product was created.
  • name : The name of the product.

Relationship​

  • belongsTo : The user who owns the product. User is the model name we want to make the relationship then in the relationship fields is the field in current schema that i map and the references is the field in the other schema field that i map.It references the ID field on the user.
  • belongsToId : The id of the user who owns the product.
  • updates : The updates made to the product.

Product Schema​

model Product {
id String @id @default(uuid())
createdAt DateTime @default(now())
name String
belongsTo User @relation(fields: [belongsToId], references: [id])
belongsToId String
}

Error​

If schema show error then run blow command to format the schema.

npx prisma format

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.

  • 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.
  • 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.
    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.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.So yeah, there's nothing stopping you.They just obviously can't be named the same.

Update Schema​

Products can have updates. So products belong to updates. Updates have many fields, one is called status. Because status is a finite set of options, we created an ENUM to represent our status. Think of an enum value types as "one-of-these". So the value must be one of the values in the ENUM instead of being any other random string.

enum UPDATE_STATUS {
IN_PROGRESS
LIVE
DEPRECATED
ARCHIVED
}

model Update {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime

title String @db.VarChar(255)
body String
status UPDATE_STATUS @default(IN_PROGRESS)
version String?
asset String

productId String
product Product @relation(fields: [productId], references: [id])
updatePoints UpdatePoint[]
}

Update Points​

model UpdatePoint {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime

name String @db.VarChar(255)
description String

updateId String
update Update @relation(fields: [updateId], references: [id])
}