Forgive my ignorance on creating schema. I am attempting to add a one to many relationship to a model with a compound ID. Here is that model:
model UserPlantSpeciesInventoried {
userId String
user User @relation("user", fields: [userId], references: [id])
plantSpeciesId String
plantSpecies PlantSpecies @relation("plantSpecies", fields: [plantSpeciesId], references: [id])
inventoryReadyByDate InventoryReadyByDate[]
@@id([userId, plantSpeciesId])
}
This model has it’s ID autogenerated. It is a unique link between a user and a "Plant Species", indicating it is in the user’s inventory.
The user will have some inventory ready at different times. For example, they may have 3 plants ready in July, and 2 in August. This in my opinion would be handled well by another model here:
model InventoryReadyByDate {
id String @id @default(cuid())
userPlantSpeciesInventoried UserPlantSpeciesInventoried @relation(fields: [userPlantSpeciesInventoriedId], references: [userId_plantSpeciesId])
userPlantSpeciesInventoriedId String
quantity Int @default(0)
dateReadyBy DateTime
}
However Prisma throws this error
Error validating: The argument
references
must refer only to
existing fields in the related modelUserPlantSpeciesInventoried
.
The following fields do not exist in the related model:
userId_plantSpeciesId
creating a custom ID name does not resolve this error. Thanks for helping.
2
Answers
Thank you @Nelloverflow for helping me figure this out. I needed to pass both ids that make up the compound Id.
I think the issue might just be in your syntax for Multi-field relations in relational databases
which should not be:
but instead: