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.

dbgenerated function cause Null constraint error in MySQL

See original GitHub issue

Bug description

Some dbgenerated functions work and others produce errors. The error is not on the database side, but on Prisma Client side.

How to reproduce

✅ no error:

model A {
  id Bytes @id @default(dbgenerated("(uuid_to_bin(uuid()))")) @db.Binary(16)
}

❌ error:

model B {
  id Bytes @id @default(dbgenerated("(uuid_to_bin(uuid(), 1))")) @db.Binary(16)
}

error detail:

Null constraint violation on the (not available)
  code: 'P2011',
  clientVersion: '3.12.0',
  meta: { constraint: null }

Expected behavior

Because the value is generated on the db side, there is no need to detect errors in Prisma Client.

Prisma information

datasource db {
  provider             = "mysql"
  url                  = env("DATABASE_URL")
  referentialIntegrity = "prisma"
}

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["referentialIntegrity", "fullTextIndex", "fullTextSearch", "interactiveTransactions", "filterJson"]
}

model A {
  id Bytes @id @default(dbgenerated("(uuid_to_bin(uuid()))")) @db.Binary(16)
}

model B {
  id Bytes @id @default(dbgenerated("(uuid_to_bin(uuid(), 1))")) @db.Binary(16)
}
export const test = async () => {
  await prisma.a.create({ data: {} })
  await prisma.b.create({ data: {} })
}

Environment & setup

  • OS: Mac OS
  • Database: MySQL
  • Node.js version: v16.14.0

Prisma Version

prisma                  : 3.12.0
@prisma/client          : 3.12.0
Current platform        : darwin
Query Engine (Node-API) : libquery-engine 22b822189f46ef0dc5c5b503368d1bee01213980 (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli 22b822189f46ef0dc5c5b503368d1bee01213980 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine    : introspection-core 22b822189f46ef0dc5c5b503368d1bee01213980 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary           : prisma-fmt 22b822189f46ef0dc5c5b503368d1bee01213980 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash    : 22b822189f46ef0dc5c5b503368d1bee01213980
Studio                  : 0.459.0
Preview Features        : referentialIntegrity, fullTextIndex, fullTextSearch, interactiveTransactions, filterJson

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sidvenucommented, Oct 10, 2022

@janpio done - #15727

1reaction
qhnucommented, May 18, 2022

Thank you for your quick response. We have confirmed that the following version works correctly.

  "dependencies": {
    "@prisma/client": "3.15.0-dev.20",
    "prisma": "3.15.0-dev.20"
  }
prisma:info Starting a mysql pool with 9 connections.
prisma:query BEGIN
prisma:query SELECT uuid_to_bin(uuid()) AS `id`
prisma:query INSERT INTO `ok`.`A` (`id`) VALUES (?)
prisma:query SELECT `ok`.`A`.`id` FROM `ok`.`A` WHERE `ok`.`A`.`id` = ? LIMIT ? OFFSET ?
prisma:query COMMIT
prisma:query BEGIN
prisma:query SELECT uuid_to_bin(uuid(), 1) AS `id`
prisma:query INSERT INTO `ok`.`B` (`id`) VALUES (?)
prisma:query SELECT `ok`.`B`.`id` FROM `ok`.`B` WHERE `ok`.`B`.`id` = ? LIMIT ? OFFSET ?
prisma:query COMMIT

No errors. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

MYSQL ignores not null constraint, inserts null values into ...
I have learnt how MYSQL NOT NULL CONSTRAINT, and the SQL STRICT MODE ... explicit NULL values into the column, MYSQL will throw...
Read more >
Prisma schema API (Reference)
Specify a MySQL data source. In this example, the target database is available with the following credentials: User: johndoe; Password: mypassword ...
Read more >
The ultimate guide on DB-generated IDs in JPA Entities
The ultimate guide on DB-generated IDs in JPA Entities. ... If sequences are not supported (i.e., we use MySQL), it will use TABLE...
Read more >
MySQL NOT NULL Constraint
It's a good practice to have the NOT NULL constraint in every column of a table unless you have a good reason not...
Read more >
Prisma 3.15.0 Release - GitClear
Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, ... dbgenerated function cause Null constraint error in MySQL ...
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