Course

Defining our Schema

Now that we have Prisma set up and connected to our Supabase database, we can define our data models in the Prisma schema.

How to determine the schema

Before diving into the code, it's essential to take a step back and analyze the requirements of our application. Let's break down the ideas and figure out how to structure our schema.

You can follow 3 steps to help you write the schema:

  1. Analyze: Identify the entities in your application.
  2. Define: Define the attributes for each entity.
  3. Connect: Establish the relationships between the entities.

Analyzing the Application

If you take a look through our application, you see it revolves around three main entities:

  • Products
  • Reviews
  • Images

Here's what we know:

have a name, description, price, and a list of associated images. are associated with a product and contain a name, rating, and content. have a URL and are associated with a product.

To design an effective schema, we need to identify these entities, or , their attributes, and the relationships between them.

Defining Attributes

Next, let's define the attributes for each entity:

Product:

: A unique identifier for each product. (primary key)

: The name of the product. (a String)

: The price of the product. (a Float)

: A description of the product. (a String)

: The category of the product. (a String)

Review:

: A unique identifier for each review. (primary key)

: The name of the reviewer. (a String)

: The rating given by the reviewer. (an Integer)

: The content of the review. (a String)

Image:

: A unique identifier for each image. (primary key)

: The URL of the image. (a String)

Connecting Entities

Now, let's determine the relationships between the entities:

can have multiple , and a review belongs to a single product. - This is a relationship between Product and Review.

can have multiple , and an image belongs to a single product. - This is a relationship between Product and Image.

These relationships can be represented using the @relation attribute in Prisma.

Insight

When designing the schema, it can be helpful to write the attributes and relationships down on paper or a whiteboard. This can help you visualize the structure of your database and ensure you haven't missed anything.

Putting It All Together

Based on our analysis, we can create the following Prisma schema, step by step:

model Product {

    }
    model Review {

    }
    model Image {

    }
model Product {
      id          Int      @id @default(autoincrement())
      name        String
      description String
      price       Float
      category    String
    }
    model Review {
      id        Int     @id @default(autoincrement())
      name      String
      rating    Int
      content   String
    }
    model Image {
      id        Int     @id @default(autoincrement())
      url       String
    }
model Product {
  id          Int      @id @default(autoincrement())
  name        String
  description String
  price       Float
  category    String
  images      Image[]
  reviews     Review[]
}

model Image {
id Int @id @default(autoincrement())
url String
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
productId Int
}

model Review {
id Int @id @default(autoincrement())
name String
rating Int
content String
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
productId Int
}
Insight

is used to specify what happens when a record is deleted. In this case, when a product is deleted, all associated images and reviews will also be deleted.

Pushing the Schema

Now that we have our schema defined, we can push it to the database using the Prisma CLI. Run the following command in your terminal:

npx prisma db push

This command will create the necessary tables in your database based on the schema you defined. You should see a success message in the terminal once the migration is complete.

Conclusion

In this lesson, we analyzed our application requirements, defined the schema for our database, and established the relationships between the entities. We then pushed the schema to the database using the Prisma CLI.

In the next lesson, we'll generate our to use in our .

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Prisma Client