referentialIntegrity=prisma causes P2022 (column not exist) on updating a record
See original GitHub issueBug 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
git clone https://github.com/aq-saito/prisma-issue.git
cd prisma-issue && npm install
- Create
.env
with MySQL DATABASE_URL npx prisma db push
– (Be warned: this flushes the database)node index.js
- 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:
- Created 2 years ago
- Reactions:3
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks, we’ll keep this issue around then. I am pretty sure though that fixing one will also fix the other side.
Will be fixed via https://github.com/prisma/prisma-engines/pull/2874