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.

referentialIntegrity=prisma causes P2022 (column not exist) on updating a record

See original GitHub issue

Bug description

Using Prisma schema with referentialIntegrity = "prisma" and references with irregular column names, updating an existing record throws P2022 error The column `test.User.id` does not exist in the current database. even though the column exists in the database.

Interestingly, this problem only happens when a model’s primary key name is other than id. Changing Article.article_id to Article.id in the example schema below, the error does not occur.

How to reproduce

I set up a project for reproduction: https://github.com/aq-saito/prisma-issue

  1. git clone https://github.com/aq-saito/prisma-issue.git
  2. cd prisma-issue && npm install
  3. Create .env with MySQL DATABASE_URL
  4. npx prisma db push – (Be warned: this flushes the database)
  5. node index.js
  6. See P2022 error occurs

Expected behavior

No error occurs. (Successfully updating the record without errors)

Prisma information

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["referentialIntegrity"]
}

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

model User {
  id   Int    @id @default(autoincrement())
  name String @db.VarChar(255)
  articles Article[]
}

model Article {
  article_id Int @id @default(autoincrement())
  user_id    Int
  user User @relation(fields: [user_id], references: [id])
}
const { PrismaClient } = require("@prisma/client");

const prisma = new PrismaClient();

async function main() {
  const user = await prisma.user.create({
    data: { name: "John Doe" },
  });

  await prisma.user.update({
    where: { id: user.id },
    data: { name: "Updated" },
  });
}

main();
/home/kotas/prisma-issue/node_modules/@prisma/client/runtime/index.js:38703
          throw new PrismaClientKnownRequestError(message, e.code, this.prisma._clientVersion, e.meta);
                ^

PrismaClientKnownRequestError:
Invalid `prisma.user.update()` invocation in
/home/kotas/prisma-issue/index.js:10:21

   7   data: { name: "John Doe" },
   8 });
   9
→ 10 await prisma.user.update(
  The column `test.User.id` does not exist in the current database.
    at cb (/home/kotas/prisma-issue/node_modules/@prisma/client/runtime/index.js:38703:17)
    at async PrismaClient._request (/home/kotas/prisma-issue/node_modules/@prisma/client/runtime/index.js:40859:18)
    at async main (/home/kotas/prisma-issue/index.js:10:3) {
  code: 'P2022',
  clientVersion: '3.9.2',
  meta: { column: 'test.User.id' }
}

Query Logs

With Article.article_id schema (above):

prisma:query BEGIN
prisma:query SELECT `test`.`User`.`id` FROM `test`.`User` WHERE `test`.`User`.`id` = ?
prisma:query SELECT `test`.`Article`.`article_id`, `test`.`Article`.`user_id` FROM `test`.`Article` WHERE (1=1 AND `test`.`Article`.`user_id` IN (?))
prisma:query SELECT `test`.`User`.`id`, `test`.`Article`.`article_id` FROM `test`.`Article` WHERE 1=0
prisma:query ROLLBACK

Changing Article.article_id to Article.id:

prisma:query BEGIN
prisma:query SELECT `test`.`User`.`id` FROM `test`.`User` WHERE `test`.`User`.`id` = ?
prisma:query SELECT `test`.`Article`.`id`, `test`.`Article`.`user_id` FROM `test`.`Article` WHERE (1=1 AND `test`.`Article`.`user_id` IN (?))
prisma:query UPDATE `test`.`User` SET `name` = ? WHERE `test`.`User`.`id` IN (?)
prisma:query SELECT `test`.`User`.`id`, `test`.`User`.`name` FROM `test`.`User` WHERE `test`.`User`.`id` = ? LIMIT ? OFFSET ?
prisma:query COMMIT

Environment & setup

  • OS: Ubuntu 20.04 on WSL
  • Database: MySQL 8.0.27
  • Node.js version: v16.13.2

Prisma Version

prisma                  : 3.9.2
@prisma/client          : 3.9.2
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt bcc2ff906db47790ee902e7bbc76d7ffb1893009 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : bcc2ff906db47790ee902e7bbc76d7ffb1893009
Studio                  : 0.457.0
Preview Features        : referentialIntegrity

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
janpiocommented, Feb 14, 2022

Thanks, we’ll keep this issue around then. I am pretty sure though that fixing one will also fix the other side.

2reactions
millspcommented, May 6, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Manage relations between records with relation modes in ...
The prisma relation mode emulates some foreign key constraints and referential actions for each Prisma Client query to maintain referential integrity, using ...
Read more >
Referential actions - Prisma
Referential actions are policies that define how a referenced record is handled by the database when you run an update or delete query....
Read more >
Prisma schema API (Reference)
API reference documentation for the Prisma Schema Language (PSL). ... Sets whether referential integrity is enforced by foreign keys in the database or ......
Read more >
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 >
Using Prisma with PlanetScale
Referential actions and integrity. To support scaling across multiple database servers, PlanetScale does not allow the use of foreign key constraints, ...
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