Relations on non unique fields
Hi guys, i'm building a new db and i need it to be multilingual. in order to achieve that i've created a contents table wich will handle every text/image for each language
Thanks to the content_id column i can join on the needed text and filter by language, that will come from the client
basically
id content_id language content
1 1 en hello
2 1 it ciao
3 1 es hola
Now i can create my challenges modules and have a relation with Contents module like this
but i get this error
Error parsing attribute "@relation": The argument
references
must refer to a unique criterion in the related model. Consider adding an @unique
attribute to the field content_id
in the model Contents
.Prisma
for each relation constraint.
How can i deal with this?2 Replies
You selected the bot's instantaneous insights. Let me compile your answer right away!
The error you're encountering is because Prisma requires that the field referenced in a
@relation
attribute must be unique in the related model. In your case, you're trying to reference content_id
in the Contents
model, but this field isn't unique by itself - it's only unique when combined with language
as part of a composite unique constraint.
There are a few ways to solve this:
Option 1: Reference the composite unique fields
You need to reference both fields that make up your unique constraint:
This approach requires you to store the language in the Challenges
model as well.
Option 2: Make content_id unique
If you want to keep your current structure, you could make content_id
unique in the Contents
model:
However, this would change your data model significantly, as you could no longer have multiple languages for the same content_id.
Option 3: Use a join model
Create an intermediate model to handle the many-to-many relationship between challenges and contents.
This is a common issue when working with relations in Prisma. As noted in the documentation, "The argument references
must refer to a unique criterion in the related model" One-to-many relations.
For MySQL specifically, Prisma enforces this uniqueness requirement since version 4.0.0 What's new in Prisma? (Q2/22).
Given your multilingual requireme
nts, Option 1 is likely the most appropriate solution as it maintains your data model while satisfying Prisma's constraints.
🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly.
In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.Prisma AI, i can't use both solution 1 or 2