An operation failed because it depends on one or more records that were required but not found (Prisma P2025)

Filed under Supabase & databases

Most likely causeAbout 10 min · 6 steps

Your code asked Prisma to update, delete, or connect a record that is not in the database — a wrong or stale ID, a type mismatch, or a row in a different database.

Read the cause line
Verify the ID and its type
Check you are in the right database

Fastest route

Give your AI this prompt

Copy it, replace the bracketed part with your error, and send it.

Preview the prompt
I am getting Prisma error P2025: "An operation failed because it depends on one or more records that were required but not found."

The full error, including the cause line in braces: [paste the full error].
The code that throws it: [paste the Prisma call].
The relevant model from my schema.prisma: [paste the model, including the ID field type].

Please: 1) tell me, from the cause line, which operation failed — update, delete, or connect, 2) check whether the ID I pass matches the schema's type and is converted correctly if it comes from a URL or form, 3) check whether the code assumes a related record exists for a connect, and show me how to create or verify it first, 4) add graceful P2025 handling so a missing record produces a clear message or a 404 instead of a crash.

Constraints: do not change my database schema, do not change which database the app connects to, and do not delete any data. Show me each file you change.

Fix it yourself

Follow the repair

  1. Read the cause line under the error. The full error ends with a specific sentence in braces. "Record to update not found" points at an update; "Record to delete does not exist" at a delete; wording about records "needed to connect" points at a nested connect. That sentence decides which of the steps below applies — do not skip it and guess.
    Invalid `prisma.post.update()` invocation:
    An operation failed because it depends on one or more
    records that were required but not found.
    { cause: "Record to update not found." }
  2. Confirm the record exists — in the database your app actually uses. Open the table in Prisma Studio or your database dashboard and look for the exact ID. Before concluding anything, check which database that is: the DATABASE_URL in the environment your app runs with decides where it looks, and local, preview, and production are usually three different databases with three different sets of rows. A row that exists in production tells you nothing about the development database your preview is querying.
  3. Check the ID itself: value and type. Log the exact ID at the moment of the failing call. Then compare it with your schema in schema.prisma. If the ID field is an Int, the value from a URL or form arrives as a string and must be converted: where: { id: Number(idFromUrl) }. If the ID is a cuid or uuid string, make sure you are not passing an email, a slug, or a display name where the ID belongs — AI-generated code mixes those up constantly.
  4. For update and delete: look before you leap, and answer gracefully. If the row might legitimately be gone, check first and turn "not found" into a clean response instead of a crash:
    const post = await prisma.post.findUnique({ where: { id } });
    if (!post) {
      // show a "that post no longer exists" message, or return a 404
      return notFound();
    }
    await prisma.post.update({ where: { id }, data: { /* ... */ } });
    Where "create it if it is missing, update it if it is there" is actually what you want, Prisma's upsert does both in one call and never throws P2025 for the missing case.
  5. For a failed connect: create or find the related record first. A nested write like the one below requires user 42 to exist at the moment it runs:
    await prisma.post.create({
      data: {
        title: "Hello",
        author: { connect: { id: 42 } }, // fails with P2025 if user 42 is not there
      },
    });
    Create the parent record first (or look it up and fail with a clear message if it is missing). The connectOrCreate variant — author: { connectOrCreate: { where: { id: 42 }, create: { /* ... */ } } } — is built for exactly this, when creating the related record on the spot makes sense.
  6. Handle P2025 in code so users never see it raw. Whatever the root cause turns out to be, wrap risky calls so a missing record becomes a friendly outcome, not a 500 page:
    import { Prisma } from "@prisma/client";
    
    try {
      await prisma.post.delete({ where: { id } });
    } catch (e) {
      if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === "P2025") {
        // Already gone — for a delete, that is often success in disguise.
      } else {
        throw e;
      }
    }
    For a delete especially, "already deleted" usually means the user's goal is achieved. Log the event, show success, move on.
Why this happensOptional explanation

P2025 is Prisma's "depends on a record that is not there" error. It fires on operations that assume a row exists: update and delete aim at one specific row and fail if it is gone, and a nested connect — "attach this post to user 42" — fails if user 42 is not there to attach to. The generic message ("An operation failed because it depends on one or more records that were required but not found") is deliberately vague, which is why the cause underneath it matters: Prisma always appends a more specific sentence, and that sentence is the real error.

For people building with AI tools, the trigger is usually mundane. The app passes an ID that came from the wrong place — a placeholder, a stale page, a string where the database expects a number — or the app is pointed at a different database than the one you are looking in, so the row genuinely exists, just not where the app is searching. Rows also vanish legitimately: someone deleted the record in the dashboard, a seed script rebuilt the data, or a previous delete in the same flow already removed it.

  • Wrong or stale ID: the code passes an ID that was never real, or belonged to a row that has been deleted.
  • Wrong type: the schema ID is a number (Int) but the code passes a string, or the other way round — route params and form values always arrive as strings.
  • Wrong database: you are viewing the production table while the app is connected to a development database, or the reverse.
  • Connect without the parent: a nested create connects to a related record that was never created first.
  • Deleted a moment ago: an earlier step in the same flow already deleted the row — a race your own code created.

Quick answers

Does P2025 always mean the row I updated was deleted?

Not always — the cause line decides. Prisma appends a specific sentence: 'Record to update not found' for updates, similar wording for deletes, and a message about records needed to connect for nested writes. A missing related record you tried to connect to is a different problem from a missing row you tried to update, and they are fixed differently.

The record is right there in my database. Why does Prisma say it is missing?

Almost always one of three things: the ID is stale or wrong, the ID type does not match the schema (a string passed where an Int is expected — URL and form values are always strings), or your app is connected to a different database than the one you are viewing, which is common with separate local, preview, and production databases.

Is it okay to just catch P2025 and ignore it?

If the same end result is acceptable, yes — especially for delete, where 'already gone' means the goal is achieved. Catch the error, check e.code === 'P2025', log it, and respond as success or a clean 404. What you should not do is swallow it silently on updates, where it can hide a real ID bug.

One fix, every Friday.

One real error, explained in plain English, with the fix — in your inbox every Friday. No spam, no jargon, unsubscribe anytime. Every issue closes with a verse and a word of encouragement.