prisma: Migration failed to apply cleanly to the shadow database

Bug description

After manually correcting a broken migration and committing it to the DB, I’m now getting the following error when I try to create a new migration with prisma migrate dev --create-only

Error: P3006

Migration `20220425205658_add_reference_to_model` failed to apply cleanly to the shadow database.
Error:
insert or update on table "reference" violates foreign key constraint "reference_model_id_fkey"
   0: sql_migration_connector::flavour::postgres::sql_schema_from_migration_history
             at migration-engine/connectors/sql-migration-connector/src/flavour/postgres.rs:354
   1: migration_core::api::DevDiagnostic
             at migration-engine/core/src/api.rs:108

But when I run prisma migrate status it returns Database schema is up to date!, so it shouldn’t be returning this error.

How to reproduce

Expected behavior

If a migration is successfully added to the DB, it shouldn’t still be returning errors.

Prisma information


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

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["nativeTypes", "interactiveTransactions"]
  binaryTargets   = ["native", "rhel-openssl-1.0.x"]
}

Environment & setup

  • OS: MacOS 10.15.7
  • Database: PostgreSQL
  • Node.js version: v14.18.1

Prisma Version

3.4.2

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 16 (7 by maintainers)

Most upvoted comments

If you really can not reset the database, then you could probably manually delete the last entry in _prisma_migrations (which records the exeution with a file hash) and then use https://www.prisma.io/docs/guides/database/production-troubleshooting#option-2-manually-complete-migration-and-resolve-as-applied - this will update the file hash to the now current one and still record the migration as applied.

reference_model isn’t the actual name of my model. It’s just the name I’m using for examples. The names match in my actual migration

Looks like I’ll be using migrate diff command from now on to manually create new migrations, since that still works. Is there any way to fix the shadow DB without resetting the actual DB?

Looks like that worked thanks!

It looks like the error is actually from adding creator_id in the INSERT statement, cause the error is showing

insert or update on table "product_collections" violates foreign key constraint "product_collections_creator_id_fkey"

I was confused because the original error was caused by the other foreign key.

The following fixes the error, but unfortunately now I can’t fix it because it says “the migration xxx.xxx.xxx was modified after it was applied. We need to reset the PostgreSQL database”

INSERT INTO "brands" (
  id,created_at,updated_at,name
)
VALUES ('zzz.zzz.zzz', NOW(), NOW(), 'test')
ON CONFLICT (id) DO NOTHING;

Is there a way to “trick” it into thinking I haven’t changed the migration?

Hm, I can not reproduce this @jeremygottfried: https://github.com/janpio/13018/tree/master/prisma/migrations Created a new project, added the previous schema (without your new additions), modified the migration as you did, applied it, then added another field and migrated again - no problems for me.

Am I missing something?