`Error: Unique constraint failed on the (not available)`

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:26
  • Comments:27 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
moaazsidatcommented, Nov 24, 2022

This issue here is about the error message, which can not provide the actual column name as it is not provided by PlanetScale/Vitess

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 and uuid (where the uuid is generated application side)

Failing with email when both email and uuid are duplicates:

target: {REDACTED}.-.primary: vttablet: rpc error: 
code = AlreadyExists desc = Duplicate entry 'testing@gmail.com' 
for key 'users.users_email_key' (errno 1062) (sqlstate 23000) 
(CallerID: planetscale-admin): Sql: "insert into 
users(email, first_name, `name`, slug, uuid, updated_at, meta) 
values (:vtg1, :vtg2, :vtg3, :vtg4, :vtg5, :vtg6, :vtg7)", BindVars: {REDACTED}

Failing with uuid when only uuid is a duplicate:

target: {REDACTED}.-.primary: vttablet: rpc error: 
code = AlreadyExists desc = Duplicate entry 'abc123' 
for key 'users.users_uuid_key' (errno 1062) (sqlstate 23000) 
(CallerID: planetscale-admin): Sql: "insert into users(email, first_name, 
`name`, slug, uuid, updated_at, meta)
values (:vtg1, :vtg2, :vtg3, :vtg4, :vtg5, :vtg6, :vtg7)", BindVars: {REDACTED}

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.

3reactions
janpiocommented, Mar 12, 2022

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)

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQLite3 UNIQUE constraint failed error - Stack Overflow
You get a UNIQUE constraint failed error when the data that you are inserting has an entry which is already in the corresponding...
Read more >
SQLite Error: UNIQUE constraint failed - Node-RED Forum
The error says that you are trying to add a record to a table with a value for a field that is defined...
Read more >
How To Handle Prisma Unique Constraints with a Friendly Error
Often, your data model will have a unique constraint to prevent duplicate records. ... A Character has to have a unique name. No...
Read more >
"UNIQUE constraint failed" error with UPSERT and UPDATE ...
"UNIQUE constraint failed" error with UPSERT and UPDATE trigger that does INSERT OR REPLACE · (1) By Even Rouault (rouault) on 2022-10-16 18:06: ......
Read more >
mailmng-outgoing failed: ERROR:__main__:UNIQUE ...
One of the following errors is displayed: PLESK_ERROR: mailmng-outgoing... ... mailmng-outgoing failed: ERROR:__main__:NOT NULL constraint ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found