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_fkeyhas to be unique in the following namespace: on modelImagefor primary key, indexes, unique constraints and foreign keys. Please provide a different name using themapargument.
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 :)