relationshipprisma

Relationships in Prisma ORM


I'm trying to better understand relationships in Prisma ORM, from the docs:

model User {
 id Int @id @default(autoincrement())
 posts Post[]
 profile Profile?
}

model Profile {
 id Int @id @default(autoincrement())
 user User @relation(fields: [userId], references: [id])  // this references the id field in User model??
 userId Int @unique // relation scalar field (used in the `@relation` attribute above)
}

the [id] in user User @relation(fields: [userId], references: [id] does the [id] reference the id field in User model??


Solution

  • Short answer: Yes. Long answer: Yes, fields: [userId] is just an imaginary field prisma uses internally.

    To see more read: Relations