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.

Cannot update a table - code: 'P2021'

See original GitHub issue

Bug description

I cannot update a table in my postgres database with Prisma. I can read it, create but cannot update this particular table. I can update this table with an SQL query. So I don’t believe that the problem is the table or the schema. This seems to be a bug in Prisma.

unknown database error:  { [Error:  
Invalid `prisma.user.update()` invocation: 
 
 
  The table `(not available)` does not exist in the current database.] 
  code: 'P2021', 
  clientVersion: '3.10.0', 
  meta: { table: '(not available)' } } 

How to reproduce

This prisma query fails with the above error:

await db.user.update({
      where: { id: uid },
      data: {
        avatar: uuid,
        images: {
          create: [
            {
              id: uuid,
            },
          ],
        },
      },
    });

This prisma query succeeds (provided I’ve already created this row):

await db.image.update({
    where: { id: uuid },
    data: {
      userId: uid,
    },
  });

Other queries on the user table succeed: db.user.create etc

Expected behavior

I expect an update query to succeed. And if I does fail I expect an error message that can tell me why it has failed. This error message is telling me the table '(not available)' doesn’t exist. Which is just false.

Prisma information

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

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


model User {
  insertedAt       DateTime            @default(now()) @map("inserted_at")
  updatedAt        DateTime            @updatedAt @map("updated_at")
  id               String              @id @default(cuid()) 
  firstName        String              @map("first_name") @db.VarChar(40) 
  lastName         String              @map("last_name") @db.VarChar(40) 
  email            String              @unique @db.VarChar(255) 
  title            String?             @db.VarChar(100) 
  bio              String?             @db.VarChar(600) 
  avatar           String?             @db.VarChar(36) 
  images           Image[]
  
  @@map("users")
}


model Image {
  insertedAt DateTime @default(now()) @map("inserted_at")
  updatedAt  DateTime @updatedAt @map("updated_at")
  id         String   @id 
  userId     String   @map("user_id")
  user       User     @relation(fields: [userId], references: [id], onDelete: Restrict, onUpdate: Cascade)

  @@index([userId])
  @@map("images")
}

Environment & setup

  • OS: Mac OS 12.2.1
  • Database: PostgreSQL
  • Node.js version: 16.14.0

Prisma Version

prisma                  : 3.10.0
@prisma/client          : 3.10.0
Current platform        : darwin-arm64
Query Engine (Node-API) : libquery-engine 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Migration Engine        : migration-engine-cli 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/migration-engine-darwin-arm64)
Introspection Engine    : introspection-core 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/introspection-engine-darwin-arm64)
Format Binary           : prisma-fmt 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/prisma-fmt-darwin-arm64)
Default Engines Hash    : 73e60b76d394f8d37d8ebd1f8918c79029f0db86
Studio                  : 0.458.0
Preview Features        : referentialIntegrity

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:20 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
fergusmeiklejohncommented, Mar 14, 2022

thanks as you suggest, the fix is to remove referentialIntegrity = "prisma" rebuild the client then put it back in and rebuild the client again 😃

I’m using this is in a postgres DB because I’m in the process of migrating to PlanetScale and I want to confirm things work before migrating

1reaction
fergusmeiklejohncommented, Jul 4, 2022

sorry! It’s Supabase

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 >
P2021 OBD-II Trouble Code: Intake Manifold Runner Position ...
P2021 is a diagnostic trouble code (DTC) for "Intake Manifold Runner Position Sensor/Switch Circuit Low Bank 2". This can happen for multiple reasons...
Read more >
('06-'08) - 2007 - P2021 - what's this means?
I saw the trouble code sticky but it does not cover this code. I found on clubwrx someone saying it was "P2021 -...
Read more >
Oracle global vs. partition level statistics CBO usage
Gathering on the whole table can be very long and doesn't 'scale' because the duration will ... partition P2021 values less than (date...
Read more >
January 2021 Update of the Hospital Outpatient Prospective ...
Table 2 of CR 12120 lists the long descriptors and status indicators for the codes. CPT codes 0227U through 0239U have been added...
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