Prisma Migrate: false positive drift detection on `foreign keys` when is used with an existing database
See original GitHub issueBug 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:
- Export just the data with
pg_dump
prisma migrate dev --name initial-migration --create-only
and pressy
to allow my database to be formatted- 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
prisma init
prisma introspect
- theschema.prisma
is generatedprisma generate
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:
- Created 2 years ago
- Comments:14 (9 by maintainers)
Top GitHub Comments
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.