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.

Prisma Migrate: false positive drift detection on `foreign keys` when is used with an existing database

See original GitHub issue

Bug description

When trying to use Prisma with an existing database I can get the migrations in sync without being forced to reset the database. I have a big DB schema but I will refer just to one table called user, because the issue is the same for all foreign keys and unique indexes.

After I run prisma migrate dev --name initial-migration --create-only, I get the following error:

? Drift detected: Your database schema is not in sync with your migration history.

[+] Added tables
  - user

[*] Changed the `user` table
  [+] Added unique index on columns (email)
  [+] Added foreign key on columns (profileId)

We need to reset the PostgreSQL database "my_database" at "localhost:5432".
Do you want to continue? All data will be lost. › (y/N)

The closest I got to the desired behaviour was:

  1. Export just the data with pg_dump
  2. prisma migrate dev --name initial-migration --create-only and press y to allow my database to be formatted
  3. Import my data back

Now everything is working but I don’t want to do this in production as we have a database with 100GB+ of data and I am not sure if something was affected.

Another weird thing is that, I have compared the database schema created by Prisma with my old one and no change. Same for schema.prisma. So I am inclined to think that it’s a bug.

Extra details

  • After I fixed everything, as I described above, I’ve decided to delete the migrations + the prima migration table. And to try one more time to see if the database schema was the problem but I got the same error. So it seems that migrate is rejecting any foreign key if it’s not in its migration files. Just an assumption, maybe it will help you.
  • I use DataGrip as a viewer for the data and even with their differences viewer tool I couldn’t detect any difference between my old schema and the one pushed by Prisma.

PS: I can fix the “Added unique index on columns” error by changing the index name. But the one with foreign key I have no clue on what’s wrong. As stated above, I have double checked my constrain vs the one generated by Prisma. 0 differences.

How to reproduce

Steps followed

  1. prisma init
  2. prisma introspect - the schema.prisma is generated
  3. prisma generate
  4. prisma migrate dev --name initial-migration --create-only

Expected behavior

Is expected that Prisma will generate the migration file after running prisma migrate dev --name initial-migration --create-only.

Prisma information

A simple table of users with an id, email (w/ a unique constraint) and a profileId which is a foreign key to the second table called profiles which contains an id and a name field.

Environment & setup

  • OS: Mac OS
  • Database: PostgreSQL
  • Node.js version: v14.17.2

Prisma Version

Tested with v2.23.0 until v2.26.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
janpiocommented, Sep 15, 2021

Wow, what an amazing post. I just skimmed it, someone else will read it in more detail, but a thought: Are you trying to baseline your database in step 9? https://www.prisma.io/docs/guides/database/developing-with-prisma-migrate/baselining Or are you just trying to use the tools you know about in Prisma land and then you end up in the situation you describe?

1reaction
Gamotecommented, Sep 15, 2021

Wow, what an amazing post. I just skimmed it, someone else will read it in more detail, but a thought: Are you trying to baseline your database in step 9? https://www.prisma.io/docs/guides/database/developing-with-prisma-migrate/baselining Or are you just trying to use the tools you know about in Prisma land and then you end up in the situation you describe?

@janpio Yup, I’m trying to baseline.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Drift detected after prisma migration dev and wants to reset the ...
Gamote mentioned this issue on Sep 15, 2021. Prisma Migrate: false positive drift detection on foreign keys when is used with an existing...
Read more >
Migration troubleshooting in development - Prisma
This guide describes how to resolve issues with Prisma Migrate in a development environment, which often involves resetting your database.
Read more >
Available CRAN Packages By Name - Microsoft
Available CRAN Packages By Name ; ACD, Categorical data analysis with complete or missing responses ; ACDm, Tools for Autoregressive Conditional Duration Models....
Read more >
Untitled
Foreign body in trachea or esophagus. Salmo bc real estate for sale. Grade de general en france, Wildwood crest logo, Sns bank app...
Read more >
Database constraints considered harmful?
It was much easier to ensure at least basic data integrity on the DB layer than having different implementations with a potential for...
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