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 keeps dropping and adding Foreign Keys

See original GitHub issue

Bug description

Any time a new migration is created, prisma migrate will try to drop foreign keys and recreate them, all migrations are being deployed successfully, but it seems there is a logic issue regarding foreign keys.

How to reproduce

  1. Run prisma migrate dev, It will deploy the DB schema.
  2. Run prisma migrate dev, A new migration will be created even though there is no changes in the Schema

Expected behavior

It should not create a new migration since the previous one already applied the migration.

Prisma information

model Issue {
  // hiding the rest of the fields
  id                        String             @id @default(uuid())
  Address                   String
  address                   Address            @relation(fields: [Address], references: [id])
  @@index([Address], name: "Address")
}

model Note {
 // hiding the rest of the fields
  id             String           @id @default(uuid())
  User           String
  user           User             @relation(fields: [User], references: [id])
  @@index([User], name: "User")
}

model PromotionReference {
  // hiding the rest of the fields
  id                String           @id @default(uuid())
  Promotion         String
  promotion         Promotion        @relation(fields: [Promotion], references: [id])
  
  @@index([Promotion], name: "Promotion")
}

model VerificationToken {
  id        String    @id @default(uuid())
  userId    String    @unique
  user      User?     @relation(fields: [userId], references: [id])
}

model Address {
  id             String           @id @default(uuid())
  issue          Issue[]
}

model User {
  id                      String               @id @default(uuid())
  notes                   Note[]
  verificationToken       VerificationToken?
}

and Migration SQL

-- DropForeignKey
ALTER TABLE `Issue` DROP FOREIGN KEY `Issue_Address_fkey`;

-- DropForeignKey
ALTER TABLE `Note` DROP FOREIGN KEY `Note_User_fkey`;

-- DropForeignKey
ALTER TABLE `PromotionReference` DROP FOREIGN KEY `PromotionReference_Promotion_fkey`;

-- DropForeignKey
ALTER TABLE `VerificationToken` DROP FOREIGN KEY `VerificationToken_userId_fkey`;

-- AddForeignKey
ALTER TABLE `Issue` ADD CONSTRAINT `Issue_Address_fkey` FOREIGN KEY (`Address`) REFERENCES `Address`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Note` ADD CONSTRAINT `Note_User_fkey` FOREIGN KEY (`User`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `PromotionReference` ADD CONSTRAINT `PromotionReference_Promotion_fkey` FOREIGN KEY (`Promotion`) REFERENCES `Promotion`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `VerificationToken` ADD CONSTRAINT `VerificationToken_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

Environment & setup

  • OS: OSX Monterey v12.1 (Intel)
  • Database: MySQL v5.7.27
  • Node.js version: 14.17.4

Prisma Version

prisma                  : 3.7.0
@prisma/client          : 3.7.0
Current platform        : darwin
Query Engine (Node-API) : libquery-engine 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine    : introspection-core 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary           : prisma-fmt 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash    : 8746e055198f517658c08a0c426c7eec87f5a85f
Studio                  : 0.445.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tarazenacommented, Dec 29, 2021

@janpio I’m not using any feature flags, also this is not the entire schema.

datasource db {
  provider = "mysql"
  url      = env("DB_ENDPOINT")
}
0reactions
paymogcommented, Jul 11, 2022

This is happening to me as well. Haven’t been able to nail down why it’s happening though

Read more comments on GitHub >

github_iconTop Results From Across the Web

Customize a migration file - Prisma
By default, renaming a field in the schema results in a migration that will: CREATE a new column (for example, fullname ); DROP...
Read more >
Why isn't referentialIntegrity = "prisma" preventing foreign keys ...
I have copied your schema, and I have follow this tutorial, and all works properly. In the image you can see that the...
Read more >
13.1.18.5 FOREIGN KEY Constraints - MySQL :: Developer Zone
A foreign key relationship involves a parent table that holds the initial ... Creating a foreign key constraint requires the REFERENCES privilege on...
Read more >
3 common foreign key mistakes (and how to avoid them)
The easiest way to avoid this issue is to ensure that all columns linked to each other with foreign keys share the same...
Read more >
Understand the shadow database feature of Prisma ORM
When running a Prisma migration on a cloud-hosted database, ... The shadow database reset consists of deleting the foreign keys, indexes, ...
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