Ashik
Ashik

Reputation: 3448

Prisma2: how to design schema which has relation with Array type Model?

I faced some problems when designing the schema.

case 1:

model User {
  package: @relation(fields: [authorId], references: [id])
  packageId: Int
}

model Package {
  user User[]
}

one package can be subscribed by hundreds of users. So, I think this is the way to go. But, the problem is, when a package needs to be deleted, the user(admin) is also needed to be deleted. which we don't want

case 2:

model User {
  package Package[]
}

model package {
  author      User?  @relation(fields: [authorId], references: [id])
  authorId    Int?
}

by designing this way, when the package is deleted, the user is not deleted. but I can't connect multiple users to a package.

Upvotes: 1

Views: 426

Answers (1)

Herku
Herku

Reputation: 7666

I think you have to explicitly model the relationship from both sides to get a many-to-many relationship. After all users can subscribe to many packages and packages can have many users subscribed.

model User {
  id Int @id @default(autoincrement())
  packages Package[] @relation(references: [id])
}

model Package {
  id Int @id @default(autoincrement())
  user User[] @relation(references: [id])
}

Upvotes: 2

Related Questions