prisma: `P2002` error doesn't include meaningful `meta` with SQL Server
Bug description
I have a unique constraint on a name field in a table. When I try and update one record with the same name as another one, I correctly get a PrismaKnownClientError, but I don’t get any other useful information in meta.
So, when I do this:
prismaClient.thing.update({
where: { id: 1 },
data: { name: 'Thing 2' }
});
I get an error thrown like this:
{
code: 'P2002',
clientVersion: '4.9.0',
meta: { target: 'dbo.Thing' },
}
As you can see, the “target” is the name of the table rather than the name of the field.
If I manually a similar query against the database using Azure Data Studio like this (which tries to set the name of one Thing to the same as that of another):
UPDATE [dbo].[Thing]
SET [name] = 'Thing 2'
WHERE [dbo].[Thing].[id] = 1
I can see that SQL Server itself gives back the following message:
Violation of UNIQUE KEY constraint 'Thing_name_key'.
Cannot insert duplicate key in object 'dbo.Thing'. The
duplicate key value is (Test Thing 2).
I understand that this does not include the column name directly, but I would assume that prisma ought to be able to get the relevant column name(s) from the constraint name that is provided.
How to reproduce
Trigger a unique constraint error on update (or in fact, on create), and inspect the error object.
Expected behavior
I expect the error object meta to include something like fields, target, or constraint so that I can provide meaningful error messages myself.
See https://github.com/prisma/prisma/issues/10829 for a similar request.
Prisma information
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlserver"
url = env("SQL_SERVER_DB_URL")
shadowDatabaseUrl = env("SQL_SERVER_SHADOW_DB_URL")
}
model Thing {
id Int @id @default(autoincrement())
name String @unique
}
// Add your code using Prisma Client
const prismaClient = new PrismaClient();
await prismaClient.thing.create({ name: 'Thing' });
await prismaClient.thing.create({ name: 'Thing 2'});
// this is where the error is thrown
await prismaClient.thing.update({
where: { id: 1 },
data: { name: 'Thing 2' },
});
Environment & setup
- OS: macOS (M1 chip), Azure SQL Edge in Docker
- Database: SQL Server
- Node.js version: 16.6
Prisma Version
4.9.0
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 12
- Comments: 19 (6 by maintainers)
Just ran into this issue too. If the error was more meaningful I would’ve saved lots of time debugging.
same here
Also I’m seeing that in Postgres the
meta.targetof a compound index comes as an array and not the name of the unique index.For example if I have:
@@unique([name, code], map: "name_code_idx")The error P2002
meta.targetin Postgres is an array:["name", "code"]but in MongoDB
meta.targetis an string"name_code_idx"@janpio DB’s also relay what value is violating the unique constraint, not just the column name. In this case, it’d be good to know that it’s the user
Thing 2that’s violating the unique constraint.Thanks for looking @dev-xo, let me know if there’s any more information I can provide, or other testing you’d like me to do (in other environments / with a specific branch of prisma, etc)
Nice flex to be able to point to a feature request from August last year 🤣 Thanks!
Understood. I don’t intend to hijack the thread of the original poster, so I’ll let them make a case for a bug versus feature request.
I opened a feature request for this in the past https://github.com/prisma/prisma/issues/15046, so I’m more than happy to plug that as the “feature request” split off of this, if you think there is sufficient information outlined in that issue.
I just ran into this issue as well. I’m trying to debug it, but I feel like some kind of error message could save me a lot of time
I’ve made a similar feature request here https://github.com/prisma/prisma/issues/15046. Would love to see this come to fruition. Even just allowing us to access the raw error thrown by the DB would be great help.