Skip to content

Relationships

Course: Prisma - chapter 10

Prisma let’s you define relationships to relate tables with each other.

Section titled “Prisma let’s you define relationships to relate tables with each other.”
  1. One to One
  2. One to Many
  3. Many to One
  4. Many to Many

2. For the TODO app, there is a one to many relationship

Section titled “2. For the TODO app, there is a one to many relationship”

Updated schema
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = "postgresql://postgres:mysecretpassword@localhost:5432/postgres"
}
model User {
id Int @id @default(autoincrement())
username String @unique
password String
firstName String
lastName String
todos Todo[]
}
model Todo {
id Int @id @default(autoincrement())
title String
description String
done Boolean @default(false)
userId Int
user User @relation(fields: [userId], references: [id])
}

4. Update the database and the prisma client

Section titled “4. Update the database and the prisma client”
npx prisma migrate dev --name relationship
npx prisma generate

💡 Try exploring the prisma/migrations folder now. Do you see more migrations for the newly created realtiohsip?