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.

Migration failed to apply to shadow database

See original GitHub issue

Bug description

We are constantly getting an error when trying to apply a migration due to a schema change. The last column we added ‘slugName’ will aways generate this error when the migration is applied


Migration `20210624184346_added_slug_name` failed to apply cleanly to the shadow database. 
Error code: P3018
Error:
A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve

Migration name: 20210624184346_added_slug_name

Database error code: 1146

Database error:
Table 'prisma_migrate_shadow_db_8ffd3d5c-5d54-4052-91f9-c30857576558.applicationconfig' doesn't exist

Please check the query number 1 from the migration file.

This migration is never resolved. Even if we ignore this migration and remove this column / property, and add any new property, the same error occurs. All migrations before this step however run normally, it only appears as new migrations are added.

How to reproduce

  1. Add any property to the schema.
  2. run ‘prisma migrate dev’
  3. Get error

Expected behavior

The migration should run and column to be added to database.

Prisma information

Schema

  provider = "mysql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model ApplicationConfig {
  id                  Int             @id @default(autoincrement())
  appId               String          @unique
  androidAppId        String          @default("")
  appName             String
  slugName            String          @default("")
  accessToken         String
  versionCode         Int             @default(1)
  majorVersion        Int             @default(1)
  majoriOSVersion     Int             @default(1)
  majorAndroidVersion Int             @default(1)
  minorVersion        Int             @default(0)
  patchVersion        Int             @default(0)
  publishStatus       Boolean         @default(false)
  buildStatus         Boolean         @default(false)
  merchantAssets      MerchantAsset[]
}

model MerchantAsset {
  id            Int               @id @default(autoincrement())
  key           String
  value         String
  application   ApplicationConfig @relation(fields: [applicationId], references: [id])
  applicationId Int

  @@unique([key, applicationId], name: "MerchantAsset_key_applicationId_unique_constraint")
}

Resulting Migration file

-- AlterTable
ALTER TABLE `ApplicationConfig` ADD COLUMN     `slugName` VARCHAR(191) NOT NULL DEFAULT '';

Environment & setup

  • OS: MacOS 11.5.1 Big Sur
  • Database: MySQL 5.7.17
  • Node.js version: v16.6.1

Prisma Version

prisma               : 2.28.0
@prisma/client       : 2.26.0
Current platform     : darwin
Query Engine         : query-engine 89facabd0366f63911d089156a7a70125bfbcd27 (at ../../../usr/local/lib/node_modules/prisma/node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli 89facabd0366f63911d089156a7a70125bfbcd27 (at ../../../usr/local/lib/node_modules/prisma/node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 89facabd0366f63911d089156a7a70125bfbcd27 (at ../../../usr/local/lib/node_modules/prisma/node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt 89facabd0366f63911d089156a7a70125bfbcd27 (at ../../../usr/local/lib/node_modules/prisma/node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 89facabd0366f63911d089156a7a70125bfbcd27
Studio               : 0.417.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Corydeeeecommented, Sep 9, 2021

Hey @tomhoule , I found a way to avoid this issue today. The problem comes from using Capitalization on the mysql command for some reason. If I ran the command

ALTER TABLE `ApplicationConfig` ADD COLUMN     `slugName` VARCHAR(191) NOT NULL DEFAULT '';

This command will corrupt the database, but the command

ALTER TABLE `applicationconfig` ADD COLUMN     `slugName` VARCHAR(191) NOT NULL DEFAULT '';

Works without problem and lets the migration succeed. Again this only happens with the migrate dev command, deploy to prod still works fine.

0reactions
storagedddcommented, Aug 24, 2022

Now I found solution. Please check https://github.com/prisma/prisma/issues/14915

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migration failed to apply cleanly to the shadow database #13018
The shadow database is brought to a clean, empty state everytime you use migrate dev . What the error is saying is that...
Read more >
How to Fix "Migration failed to apply cleanly to the shadow ...
I made an update to my Prisma schema and used Prisma Migrate to keep it in sync with my database schema. I ran...
Read more >
About the shadow database - Prisma
Important: The shadow database is not required in production, and is not used by production-focused commands such as prisma migrate resolve and prisma...
Read more >
Error when migrating models to database Prisma
I tried Introspect. But, my DB currently has no tables and that threw an error. I tried npx prisma migrate save -experimental b/c...
Read more >
Impossible prisma (postgreSQL) schema migration - Render
Hello all! I'm facing an issue while trying to migrate my prisma schema: Error: P3014 Prisma Migrate could not create the shadow database....
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