question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PrismaClientUnknownRequestError in prisma.user.create(...) with trigger

See original GitHub issue

Bug description

When I try to create a new user I got this error:

prisma:info Starting a mysql pool with 9 connections.
prisma:query BEGIN
prisma:query INSERT INTO `iosef_db`.`user` (`id`,`hash`,`salt`,`password`,`email`,`token`,`tokenExpiration`) VALUES (?,?,?,?,?,?,?)
prisma:query SELECT `iosef_db`.`user`.`id`, `iosef_db`.`user`.`hash`, `iosef_db`.`user`.`salt`, `iosef_db`.`user`.`password`, `iosef_db`.`user`.`email`, `iosef_db`.`user`.`token`, `iosef_db`.`user`.`tokenExpiration` FROM `iosef_db`.`user` WHERE `iosef_db`.`user`.`id` = ? LIMIT ? OFFSET ?
prisma:query ROLLBACK
PrismaClientUnknownRequestError: 
Invalid `prisma.user.create()` invocation in
/api/src/prisma/models/User.model.ts:150:44

  147 const salt = await bcrypt.genSalt(10);
  148 const hashedPassword = await bcrypt.hash(data.password, salt);
  149 
→ 150 const user = await prisma.user.create(
  Query createOneuser is required to return data, but found no record(s).
    at cb (/api/node_modules/@prisma/client/runtime/index.js:38679:17)
    at async Function.create (/api/src/prisma/models/User.model.ts:150:26)
    at async loginController.test (/api/src/controllers/login.controller.ts:31:25)
    at async /api/node_modules/@nestjs/core/router/router-execution-context.js:46:28
    at async /api/node_modules/@nestjs/core/router/router-proxy.js:9:17 {
  clientVersion: '3.5.0'
}

How to reproduce

await prisma.user.create({
    data: {
        email: 'someone@gmail.com',
        password: 'password',
        tokenExpiration: `1643896265257`,
        token: 'eyJhbGciOiJIUz…Nn0.ouT9RiI6X0vXEkjf9rxfUhIu_UUYZ-ZkRUCuqW-9tUk',
        salt: '$2b$10$q.y4.vrHKxDYMQkeWm9xW.',
        hash: '$2b$10$Qju63Il/erAV1epJyeEk6epdX5/zD.6kC4oTTjTtlHeUpCUDfpHay'
    }
});

Expected behavior

It should create the user without error

Prisma information

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = env("MYSQL_URL")
}

model user {
  id              String @id @default("") @db.VarChar(36)
  hash            String @db.VarChar(80)
  salt            String @db.VarChar(80)
  password        String @db.VarChar(22)
  email           String @db.VarChar(50)
  token           String @db.VarChar(280)
  tokenExpiration String @db.VarChar(50)
}

Environment & setup

  • OS: Linux (Inside a docker container node:16.13.0)
  • Database: MySQL
  • Node.js version: 16.13.0

Prisma Version

3.5.0

Edit

I tried using prisma.user.createMany(...); and it works perfectly!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
pimeyscommented, Sep 8, 2022

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 SELECT does not find anything.

It is unclear can you even do this with MySQL and an ORM. MySQL does not have RETURNING for an INSERT, so we have no idea what is the generated UUID value for the record.

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;
1reaction
janpiocommented, Sep 9, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error message reference - Prisma
Prisma Client throws a PrismaClientUnknownRequestError exception if the query engine returns an error related to a request that does not have an error...
Read more >
Intermittent prisma crashes - RedwoodJS Community
api | error creating user PrismaClientInitializationError: api ... Looking at the logs, out of 303 invocations I triggered the error twice.
Read more >
https://cdn.jsdelivr.net/npm/prisma@3.5.0/prisma-c...
Sql, ...values: any[]): any; __internal_triggerPanic(fatal: boolean): any; ... this same config is passed to {@link getPrismaClient} which creates a ...
Read more >
@prisma/cli: Versions | Openbase
NewFromFloat(1.23456789) created, err := client.User.CreateOne( db.User. ... generation is always triggered to avoid issues where the Prisma Client API is ...
Read more >
Highlights - FeedsAnywhere
Prisma will use the native database upsert if: There are no nested queries in the upsert 's create and update options; The query...
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