-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PrismaClientUnknownRequestError
in prisma.user.create(...)
with trigger
#10497
Comments
Is this consistently reproducible for you? Super weird. |
I think @Datzu712 hasn't configured the model correctly and that's why it's failing.
Default is set to blank string, thus when you use create() Prisma thinks you are inserting an empty string "" as the id, thus when it tries to the select after inserting it tries to select the user with id = "" empty string and fails, and then rolls back. What happens with createMany() is that it successfully inserts like with create(), but createMany() does not try and run a SELECT query afterwards with id = "" empty string. Solution found, I think. |
However I am having a similar problem. My problem is due to the fact that my primary key includes a timestamp, and Prisma thinks mysql and postgres save the exact timestamp (with milliseconds) that Prisma sent in the INSERT query, but mysql and postgres both round up or down the timestamp when it saves it. HOWEVER when running the SELECT query mysql and postgres do not round up or down the timestamps thus having an exact timestamp comparision, but it cannot find the inserted ROW because the timestamp was rounded up in the save process. And thus Prisma rolls back the INSERT query because it did not return any values. TL;DR: Databases aproximate the timestamp when "INSERTING", but do not aproximate them when "SELECTING". I think this is a big problem. |
A practical example: Table's primary key is @@id([taxiId, arrivedAt]).
Notice the .9 after the seconds, as we all know javascript allows for milliseconds and does not aproximate to the exact second. So the database (both mysql and postgres) will round up the INSERTED timestamp, but when comparing it will use the EXACT timestamp. So even though the INSERT and SELECT have the same exact value "2022-01-27T16:49:54.9+01:00", the SELECT statement will return NO RESULTS, and Prisma will throw an error and ROLLBACK. The only possible solution if for Prisma to round up before sending the value to the database in the first place, it could do this by taking the schema.prisma @db.Timestamp into consideration, the developer would have to manually specify @db.Timestamp(0) which I've done. But Prisma does not take this into consideration, and just sends the full datetime. |
It doesn't matter in which ISO 8601 you send it as "2022-01-27T16:49:54.9+01:00" or "2022-01-27T15:49:54.9Z" both have the same problem.
You cannot use A simple workaround would be "const arrivedAt = new Date(parseVar)" and then call "arrivedAt.setMilliseconds(0)", and then send the date. Then the whole create() sequence works correctly. This workaround also proves my point, that milliseconds are causing prisma create() to fail when the primary key includes a datetime or timestamp. |
Can you please split your problem into its own issue @csanso-limit? Right now I can not apply the correct labels or ask the correct questions as this is in another user's issue which you already kind of debugged and solved. Very happy to take a look at what you write - but in its own issue with the full issue information filled out. Thanks. |
(Srry for my english) I tried adding the ID manually but I got the same error :( model test {
id String @id @db.VarChar(50)
salt String @db.VarChar(50)
email String @db.VarChar(50)
token String @db.VarChar(50)
tokenExpiration String @db.VarChar(50)
hash String @db.VarChar(80)
} const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient()
prisma.test.create({
data: {
email: 'someone@gmail.com',
tokenExpiration: `1643896265257`,
token: 'eyJh9rxfUhIu_UUYZ-ZkRUCuqW-9tUk',
salt: '$2b$10$q.y4.vrHKxDYMQkeWm9xW.',
hash: '$2b$10$Qju63Il/erAV1epJyeEk6epdX5/zD.6kC4oTTjTtlHeUpCUDfpHay',
id: '7bbf1f4a-7713-11ec-8968-0242ac120002'
}
}).then(console.log).catch(console.error); With others ORM I didn't have problems with this. |
Does anybody figure out to fix this problem? or a way to fix it? |
Resolve it. I've migrated from prisma 1 to prisma 2 and the length of Id(@id) was increased to 30 characters into prisma 2 due to cuid() generator |
I can reproduce the problem. This can be associated with #4246. But the setup with trigger to assign IDs is not very common so this might not be as straightfoward. |
Note: Alternatively, Prisma could return the data that was created but the data would be incorrect. We could make this work by disabling the fetch, see feature request #4246 |
This comment was marked as outdated.
This comment was marked as outdated.
Ok, now I can reproduce this with 4.3.1. What happens here is that we generate that empty string default value in the core, and we expect to find this new record with that value. The database generates a UUID, and our It is unclear can you even do this with MySQL and an ORM. MySQL does not have Easy reproduction with the migration: -- CreateTable
CREATE TABLE `user` (
`id` VARCHAR(36) NOT NULL DEFAULT '',
`hash` VARCHAR(80) NOT NULL,
`salt` VARCHAR(80) NOT NULL,
`password` VARCHAR(22) NOT NULL,
`email` VARCHAR(50) NOT NULL,
`token` VARCHAR(280) NOT NULL,
`tokenExpiration` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE TRIGGER insert_guid
BEFORE INSERT ON `user`
FOR EACH ROW
BEGIN
SET NEW.id = UUID();
END; |
Probably related: #14918 |
PrismaClientUnknownRequestError
in prisma.user.create(...)
with trigger
Bug description
When I try to create a new user I got this error:
How to reproduce
Expected behavior
It should create the user without error
Prisma information
Environment & setup
node:16.13.0
)Prisma Version
Edit
I tried using
prisma.user.createMany(...);
and it works perfectly!The text was updated successfully, but these errors were encountered: