Prisma, without @relation?

Viewed 33
model Post{
  //...
  place          Place[]         @relation(fields: [placeId], references: [id])
  placeId         Int
}

model Place{
  //...
  post          Post         @relation(fields: [postId], references: [id])
  postId        Int
}

when I do this,

I got this error

error: Error parsing attribute "@relation": The relation field place on Model Post must not specify the fields or references argument in the @relation attribute. You must only specif y it on the opposite field post on model Place.

how to fix this??

and I don`t understand difference between

just simple post Post (without @relation)

and

post Post @relation(...)

1 Answers

A relation is used to show a connection between two entities or models in the Prisma schema. To fix your issue, you would do something like

model Post{
  id Int @id @default(autoincrement())
  place Place[]        
}

model Place{
  id Int @id @default(autoincrement())
  post  Post  @relation(fields: [postId], references: [id])
  postId  Int
}

To learn more about relations, please do take a look at the docs

For an implicit many-to-many relation, you can do

model Post{
  id Int @id @default(autoincrement())
  place Place[]        
}

model Place{
  id Int @id @default(autoincrement())
  post  Post[]
  postId  Int
}
Related