'Prisma schema one-to-many relations from multiple tables to one

I want to acomplish such schema, where user could have some images and recipes could have some images.

I came up with solution, but it throws this error for author and recipe rows in image model. My solution is to use type column as diferentiator between Recipe and User types.

Error parsing attribute "@relation": The given constraint name images_parentId_fkey has to be unique in the following namespace: on model Image for primary key, indexes, unique constraints and foreign keys. Please provide a different name using the map argument.

My solution:

model User {
id          String       @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
images      Image[]      @relation("UserImages")
Recipe      Recipe[]     @relation("RecipeAuthor")

@@map("users")
}

model Recipe {
id       String       @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
authorId String       @db.Uuid
author   User         @relation("RecipeAuthor", fields: [authorId], references: [id])
images   Image[]      @relation("RecipeImages")

@@map("recipes")
}

model Image {
id         String     @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
type       String
parentId   String     @db.Uuid
author     User       @relation("UserImages", fields: [parentId], references: [id])
recipe     Recipe     @relation("RecipeImages", fields: [parentId], references: [id])

@@map("images")
}

Only other solution I came up with is to create multiple tables for images, something like user_images and recipe_images, but i dont really like that and I might need even more tables like this in future, so I would rather find better way. Thanks in advance for any help :)



Solution 1:[1]

Instead of using parentId for both in UserImages and ReceipeImages relation. You can separate them out and have something like parentUserId and parentReceipeId.

The following solution could potentially work:

model User {
  id     String   @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  images Image[]  @relation("UserImages")
  Recipe Recipe[] @relation("RecipeAuthor")

  @@map("users")
}

model Recipe {
  id       String  @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  authorId String  @db.Uuid
  author   User    @relation("RecipeAuthor", fields: [authorId], references: [id])
  images   Image[] @relation("RecipeImages")

  @@map("recipes")
}

model Image {
  id              String  @id @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  type            String
  parentUserId    String?  @db.Uuid
  parentReceipeId String?  @db.Uuid
  author          User?   @relation("UserImages", fields: [parentUserId], references: [id])
  recipe          Recipe? @relation("RecipeImages", fields: [parentReceipeId], references: [id])

  @@map("images")
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nurul Sundarani