prisma: `Error: Unique constraint failed on the (not available)` on PlanetScale/Vitess
Note: Please read https://github.com/prisma/prisma/issues/10829#issuecomment-1065842833 before responding to this issue.
Bug description
PlanetScale/Vitess does not provide the column name. But additionally to that not showing, we are also missing the additional fields
or constraint
string that is present in the error, helping the user to figure out what might be wrong.
How to reproduce
Cause unique constraint error on PlanetScale/Vitess
Expected behavior
More helpful error message.
Prisma information
Environment & setup
- OS:
- Database:
- Node.js version:
Prisma Version
3.7.0
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 39
- Comments: 40 (7 by maintainers)
Digging more into this seems like when a raw query is executed via PlanetScale, it does return some data about the column on which the unique constraint first fails on.
Here’s an example with a users table with unique
email
anduuid
(where theuuid
is generated application side)Failing with
email
when bothemail
anduuid
are duplicates:Failing with
uuid
when onlyuuid
is a duplicate:Overall new to the Prisma codebase, but going to start looking into where in Prisma are these errors handled. Any directions would be super helpful.
This issue here is about the error message, which can not provide the actual column name as it is not provided by PlanetScale/Vitess, as described in the original issue. There is no real solution than to fix Vitess or implement some workaround that gets the column name from the original query.
If you have any code that causes the
Unique constraint failed ...
error message although it is unexpected, this issue here is not the correct one. Please open a new issue and describe your problem. (E.g. your problem @dyeoman2 would be a good new bug report so we can look into how this can happen)Solution is to set skipDuplicates: true
eg :
await prisma.user.createMany({ data: seed_users, skipDuplicates: true, });
I was getting this issue when seeding my database : npx prisma db seed Error message :
I can confirm with
planetscale
.Facing the same issue here using Planetscale. For me it was creating the same object twice in a deep nested
create
.@moaazsidat I checked a bit and:
quaint
https://github.com/prisma/prisma-engines/blob/37eb6ab51d59be9ed1fbe46e5b95931a6e4ceac1/libs/user-facing-errors/src/quaint.rs#L12quaint
https://github.com/prisma/prisma-engines/blob/main/quaint/src/connector/mysql/error.rsStill facing the same issue.
After checking fields one at a time, I found out that pscale wanted the createdAt and updatedAt dates provided in my create request even though I had them set to automatically populate in my Prisma schema. Hope this narrows things down for you.
Let’s make this the last message about this problem here in this unrelated issue @KODerFunk, but in the past this sometimes happened when the database returned error messages in a non standard language. That can throw our logic off right now. (To respond to this, best open a new issue quickly - no need to already have investigated this further - but let’s not spam the other 30 participants in this Vitess/PlanetScale specific issues any more 😆 )
confirming that I am still running into this issue (specifically, lack of a column name in the error message when a unique constraint fails) on Planetscale.
Have also confirmed @moaazsidat’s finding that Planetscale does indeed have info in its error message indicating the column where the unique constraint fails. Could we get another look at this? Seems there should be a way to parse the column name from the Planetscale error and pull it into the prisma error.
I found my problem was sequence of
id
field, which i set an auto increment.I reset my sequence to last column id + 1. For instance, if your last one is 100, reset it to 101.
*You can find sequence name with:
Any update on this? As of right now, I have to manually check for every property uniqueness in order to send a proper message to my front end. Did you find a better workaround than this ?
I had this issue with createMany. After adding
skipDuplicates: true
, it is working now.In my case the issue was that I was doing a call to
updateMany
with two values with the same id (a violation of the constraint). I think the issue here is that the error message is helpful, but the(not available)
is missing the details of the unique constraint violation.I will try to repeat my instructions again:
I am facing the same issue, has anyone found a way to figure this out?
I am currently having the same issue while using the update request.
My workaround for this issue: I used
prisma.$executeRaw
and everything works as expected.Yeah, I have the same issue as well.
+1
+1