maybeNoT
maybeNoT
Explore posts from servers
PPrisma
Created by maybeNoT on 6/18/2024 in #help-and-questions
Pointing two relations to same field?
I have a scenario where it make sense to store informations about two 1-n relation in one field. I'm wondering if it is even possible to do something like this. Consider this example:
model User {
id Int @default(autoincrement()) @id
posts Post[] @relation(name: "rel")
}

model Post {
id Int @default(autoincrement()) @id
author1 User? @relation(name: "rel", fields: [author1Id], references: [id])
author1Id Int?
author2 User? @relation(name: "rel", fields: [author2Id], references: [id])
author2Id Int?
}
model User {
id Int @default(autoincrement()) @id
posts Post[] @relation(name: "rel")
}

model Post {
id Int @default(autoincrement()) @id
author1 User? @relation(name: "rel", fields: [author1Id], references: [id])
author1Id Int?
author2 User? @relation(name: "rel", fields: [author2Id], references: [id])
author2Id Int?
}
This shows me an error of course:
Error validating model "Post": Ambiguous relation detected. The fields `author1` and `author2` in model `Post` both refer to `User`. Please provide different relation names for them by adding `@relation(<name>).
Error validating model "Post": Ambiguous relation detected. The fields `author1` and `author2` in model `Post` both refer to `User`. Please provide different relation names for them by adding `@relation(<name>).
But ideally I would like to have relation about author1 and author2 stored separately on Post table, and informations about these posts related to authors in one field with Post[]. In other words, I want to store informations about Post related to the User in one array, doesn't matter if a User is in author1 or author2 field. Is something like this possible? - I'm using postgresql
1 replies