Prisma postgresql many-to-many multiple fields

21 views Asked by At

I'm trying to define the relationships in Prisma where:

  • There's a Post model
  • There's a Tag model
  • Each Post can have multiple primaryTags: Tag[], and multiple secondaryTags: Tag[]

So, seems like both primary and secondary tags relations would reference the same PostTag table. In one to many we'd use a named relation. Not sure how to deal with that in many to many.

model Post {
   id @id @default(cuid())
   primaryTags Tag[]
   secondaryTags Tag[]
}

model Tag {
   id @id @default(cuid())
   name String
}

model PostTag {
   postId String
   post @relation(fields: [postId], references: [id])
   tagId String
   tag @relation(fields: [tagId], references: [id])
   @@id([postId, tagId])
}

I guess, if I was doing it without Prisma, I'd set an additional property isPrimary on the PostTag table. But not sure if it's the right way to go here. Also, that's probably not doable if defining implicit many-to-many Prisma-recommended way.

One other way I could think of is to define 2 separate relationship tables - one for primary tags, one for secondary. Not sure if that's too much though.

0

There are 0 answers