@unique on optional field
and
As you can see, user_id is an optional field. However the relation REQUIRES it to be marked as unique. In doing so, I get the following error: duplicate key { user_id: null }
I imagine this will be an issue for excluded emails too, though I haven't run into it yet
12 Replies
Hey @Nik
Is there any reason why user/user_id is optional? It makes sense to be required as the player should not exists without associated user.
I believe the field cannot be both, optional and unique, if you want to keep it optional and still unique, you will be responsible for creating unique identifier for that entity
that's actually not the case here. A user is tied to whether they've signed in. And a player is only linked/created when they start their first game.
Both are optional
So they have to be optional on both sides.
So a player can be created without a user (witness another player ingame) and a user without a player (hasn't played a game yet)
Then I guess you need to create - relation
https://www.prisma.io/docs/orm/prisma-schema/data-model/relations/many-to-many-relations
Many-to-many relations | Prisma Documentation
How to define and work with many-to-many relations in Prisma.
the issue is that fundamentally the relationship is one to one, and I'd prefer not to hack it with a many-many relationship that isn't accurate
am I missing something?
ty for the help
GitHub
Support
@unique
on nullable fields for databases that support it ...It would be great if we could take advantage of Postgres's capability to express uniqueness on not null values. Currently if I add the @unique constraint on a nullable field I can't have mo...
Prisma Schema API | Prisma Documentation
API reference documentation for the Prisma Schema Language (PSL).
In mongo / mongo drivers I can ignore null values like this.
So according to my research this should be creating a sparse / partial index in MongoDB to support this. However upon
prisma db push
to a new empty database, I see the following keys:they're not marked sparse / partial which causes the issue.
THE PROBLEM
OPTIONAL @unique indexes are erroring with duplicate
null/undefined
values.
This means that two records that omit an optionally @unique field will clash
THE SOLUTION
What Prisma should be doing is setting Sparse = true on any index of an optional @unique field
But I am having to manually remember to do this for every index from an optional @unique
If we mark an OPTIONAL field foo
@unique, run:
db.collection.createIndex( { foo: 1 }, { unique:true, sparse: true } )
If we mark a REQUIRED field foo
@unique, run:
db.collection.createIndex( { foo: 1 }, { unique:true )
^ This is all prisma would have to do, currently it runs the second in both casesThis is how I'm manually editing the indexes created by Prisma:
Create a Github issue
Its your best bet I imagine so they can implement it
GitHub
[BUG] Prisma not supporting optional @unique indexes in MongoDB · I...
Bug description THE PROBLEM OPTIONAL @unique indexes are erroring with duplicate null/undefined values. This means that two records that omit an optionally @unique field will clash THE SOLUTIO...