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. Types of relationships
Section titled “1. Types of relationships”- One to One
- One to Many
- Many to One
- 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”
3. Updating the prisma schema
Section titled “3. Updating the prisma schema”
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 relationshipnpx prisma generate💡 Try exploring the
prisma/migrationsfolder now. Do you see more migrations for the newly created realtiohsip?
