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.

Postgres Migration Bug after making schema changes (P3006 Error)

See original GitHub issue

Bug description

Migrations don’t appear to be working as expected when using postgresql (tried on both 12.6 and 13). Specifically, running the first migration works fine. However, when I make new changes to the schema.prisma file, Prisma doesn’t pick up on the changes and it instead tries reapplying the migration already there and I get a P3006 error stating the relations are already created.

How to reproduce

  1. Set up some temp postgres DB using 12.6 or 13
  2. Start with boilerplate prisma schema code
  3. Run first migration npx prisma migrate dev --name init --preview-feature
  4. Make some change to the prisma.schema file (add new model, change a column name, etc)
  5. Run migration again: npx prisma migrate dev --name change --preview-feature
Console Error
Error: P3006

Migration `20210217161456_init` failed to apply cleanly to a temporary database. 
Error:
Database error: Error querying the database: db error: ERROR: relation "User" already exists
   0: sql_migration_connector::flavour::postgres::sql_schema_from_migration_history
             at migration-engine/connectors/sql-migration-connector/src/flavour/postgres.rs:270
   1: sql_migration_connector::sql_database_migration_inferrer::calculate_drift
             at migration-engine/connectors/sql-migration-connector/src/sql_database_migration_inferrer.rs:40
   2: migration_core::api::DevDiagnostic
             at migration-engine/core/src/api.rs:79

Expected behavior

I expected Prisma to generate a new migration file with the new name based on the diff and apply the changes from the diff.

Prisma information

This issue can be reproduced using the simple boilerplate prisma schema.

Schema for 1st Migration
datasource db {
  url           = env("DATABASE_URL")
  provider = "postgresql"
}
generator client {
  provider = "prisma-client-js"
}
model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  email     String   @unique
  name      String?  
}
Altered Schema for 2nd Migration
datasource db {
  url      = env("DATABASE_URL")
  provider = "postgresql"
}
generator client {
  provider = "prisma-client-js"
}
model User {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  email     String   @unique
  name      String?
}
// CHANGED: Added new table to DB
model Post {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  published Boolean  @default(false)
  title     String   @db.VarChar(255)
  author    User?    @relation(fields: [authorId], references: [id])
  authorId  Int?
}

Environment & setup

  • OS: MacOS
  • Database: PostgreSQL (12.6 and 13)
  • Node.js version: v14.1.1
  • Prisma version: 2.17

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
tomhoulecommented, Feb 18, 2021

@chelog last update — I have a PR here, it does look like we broke the MySQL case sensitivity setting detection recently, and didn’t notice because we don’t run CI on windows MySQL installations.

1reaction
jbroomercommented, Feb 18, 2021

I think it may have been a DB permissions issue for me. I had some others try it out and they were able to perform the 2nd migration. It, also, worked when I specified a shadow DB (even though I shouldn’t have had to since I was running locally with a superuser).

Thank you for looking into this though!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error when migrating models to database Prisma
My database is postgresQL and it's hosted on heroku. My DATABASE_URL is copied/pasted from the configs on heroku. Here are my .json dependencies ......
Read more >
Error message reference - Prisma
P3006. "Migration {migration_name} failed to apply cleanly to the shadow database. {error_code}Error: {inner_error}" ...
Read more >
Documentation: 15: 5.9. Schemas - PostgreSQL
Often you will want to create a schema owned by someone else (since this is one of the ... If there is no...
Read more >
Migrations - Django documentation
Migrations are Django's way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema....
Read more >
Use AWS SCT to Convert the Oracle Schema to PostgreSQL
After the migration is complete, you can create these objects on the Amazon RDS for PostgreSQL database. Run the script on the target...
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