children and favourite child (ambiguous relation)

I want to model a relationship where a parent record can have many children but there is also a 'favourite' (or 'current favourite') child out of the list of children indicated by an id reference in the parent, to the child. I realise that this is a bit of a circular relationship and care will need to be taken when removing children or changing the favourite but I think the efficiency in query performance is worth the effort. Here is a contrived example...
model Parent {
id Int @id @default(autoincrement())

favourite_child_id Int
favourite_child Child @relation(fields: [favourite_child_id], references: [id])

children Child[]

@@map("parent")
}

model Child {
id Int @id @default(autoincrement())

parent_id Int
parent Parent @relation(fields: [parent_id], references: [id])

@@map("child")
}
model Parent {
id Int @id @default(autoincrement())

favourite_child_id Int
favourite_child Child @relation(fields: [favourite_child_id], references: [id])

children Child[]

@@map("parent")
}

model Child {
id Int @id @default(autoincrement())

parent_id Int
parent Parent @relation(fields: [parent_id], references: [id])

@@map("child")
}
This according to my brain should be fine but Prisma complains
Error: Prisma schema validation - (validate wasm)
Error code: P1012
error: Error validating model "Parent": Ambiguous relation detected. The fields `favourite_child` and `children` in model `Parent` both refer to `Child`. Please provide different relation names for them by adding `@relation(<name>).
--> schema.prisma:280
|
279 | favourite_child_id Int
280 | favourite_child Child @relation(fields: [favourite_child_id], references: [id])
281 |
|
Error: Prisma schema validation - (validate wasm)
Error code: P1012
error: Error validating model "Parent": Ambiguous relation detected. The fields `favourite_child` and `children` in model `Parent` both refer to `Child`. Please provide different relation names for them by adding `@relation(<name>).
--> schema.prisma:280
|
279 | favourite_child_id Int
280 | favourite_child Child @relation(fields: [favourite_child_id], references: [id])
281 |
|
I have tried a number of iterations of this including naming the relations and adding back references etc, all of which just introduce even more validation issues. Any Ideas how I can make Prisma happy with this approach?
1 Reply
JavascriptMick
JavascriptMickOP8mo ago
ok, never mind I figured it out. I want to apologise to anybody who is triggered by this example. I know these type of relationships can be fraught and vexxed. I only seek to understand.
model Parent {
id Int @id @default(autoincrement())

favourite_child_id Int
favourite_child Child @relation("ParentHasFavourite", fields: [favourite_child_id], references: [id])

children Child[] @relation("ParentHasChildren")

@@map("parent")
}

model Child {
id Int @id @default(autoincrement())

parent_id Int
parent Parent @relation("ParentHasChildren", fields: [parent_id], references: [id])

parents_who_love_me_most Parent[] @relation("ParentHasFavourite")

@@map("child")
}
model Parent {
id Int @id @default(autoincrement())

favourite_child_id Int
favourite_child Child @relation("ParentHasFavourite", fields: [favourite_child_id], references: [id])

children Child[] @relation("ParentHasChildren")

@@map("parent")
}

model Child {
id Int @id @default(autoincrement())

parent_id Int
parent Parent @relation("ParentHasChildren", fields: [parent_id], references: [id])

parents_who_love_me_most Parent[] @relation("ParentHasFavourite")

@@map("child")
}
Want results from more Discord servers?
Add your server