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.

Detection of the foreign key names with prisma migrate

See original GitHub issue

Bug description

After updating Prisma from 2.27.0 to 2.30.0 I have this issue in my dev env:

> npx prisma migrate dev

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

The following is a summary of the differences between the expected database schema given your migrations files, and the actual schema of the database.

It should be understood as the set of changes to get from the expected schema to the actual schema.

[*] Changed the `Addresse` table
  [-] Removed foreign key on columns (companieId)
  [-] Removed foreign key on columns (userId)
  [+] Added foreign key on columns (companieId)
  [+] Added foreign key on columns (userId)

[*] Changed the `AuthDevice` table
  [-] Removed foreign key on columns (userId)
  [+] Added foreign key on columns (userId)

[*] Changed the `Balance` table
  [-] Removed foreign key on columns (companieId)
  [+] Added foreign key on columns (companieId)

[*] Changed the `Campaign` table
  [-] Removed foreign key on columns (userId)
  [+] Added foreign key on columns (userId)

[*] Changed the `CampaignHistoric` table
  [-] Removed foreign key on columns (campaignId)
  [-] Removed foreign key on columns (companieId)
  [-] Removed foreign key on columns (userId)
  [+] Added foreign key on columns (campaignId)
  [+] Added foreign key on columns (companieId)
  [+] Added foreign key on columns (userId)

[*] Changed the `Cardholder` table
  [-] Removed foreign key on columns (companieId)
  [-] Removed foreign key on columns (userId)
  [+] Added foreign key on columns (companieId)
  [+] Added foreign key on columns (userId)

[*] Changed the `Charge` table
  [-] Removed foreign key on columns (invoiceId)
  [-] Removed foreign key on columns (sourceId)
  [+] Added foreign key on columns (invoiceId)
  [+] Added foreign key on columns (sourceId)

...
// same pattern for my entire database


We need to reset the MySQL database "nacho@prod" at "localhost:3306".
Do you want to continue? All data will be lost. … no

Reset cancelled.

So I decided to update my schema, as it looks like my database changed (with npx prisma db pull ) => No changed made in the schema. I ran again npx prisma migrate dev, I got the same result described above.

I decided to move ahead and compare the table Address after reseting the database in dev.

CREATE TABLE `Addresse` (
  `id` char(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `type` enum('BILLING','SHIPPING','MAILING','HOME','PAYMENT_SOURCE_BILLING_ADDRESS') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `address1` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `address2` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `city` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `zip` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `state` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `country` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `updatedAt` datetime(3) NOT NULL,
  `createdAt` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `name` mediumtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
  `userId` char(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `companieId` char(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `companieId` (`companieId`),
  KEY `userId` (`userId`),
  CONSTRAINT `Addresse_ibfk_1` FOREIGN KEY (`companieId`) REFERENCES `Companie` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT `Addresse_ibfk_2` FOREIGN KEY (`userId`) REFERENCES `User` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

changed to:

CREATE TABLE `Addresse` (
  `id` char(30) COLLATE utf8mb4_unicode_ci NOT NULL,
  `type` enum('BILLING','SHIPPING','MAILING','HOME','PAYMENT_SOURCE_BILLING_ADDRESS') COLLATE utf8mb4_unicode_ci NOT NULL,
  `address1` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `address2` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `city` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `zip` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `state` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `country` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `updatedAt` datetime(3) NOT NULL,
  `createdAt` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `name` mediumtext COLLATE utf8mb4_unicode_ci,
  `userId` char(30) COLLATE utf8mb4_unicode_ci NOT NULL,
  `companieId` char(30) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `companieId` (`companieId`),
  KEY `userId` (`userId`),
  CONSTRAINT `addresse_ibfk_1` FOREIGN KEY (`companieId`) REFERENCES `Companie` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT `addresse_ibfk_2` FOREIGN KEY (`userId`) REFERENCES `User` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

It looks like Prisma needs to have a lower case for the name of the foreign key? Why? Addresse_ibfk_1 vs addresse_ibfk_1

How to reproduce

  1. Set a MYSQL Database with 2 table and 1 relationship. The Name of the foreign key should be UpperCase
  2. run npx prisma migrate dev

Expected behavior

IT should work, no waning like this should be triggered

[*] Changed the `Addresse` table
  [-] Removed foreign key on columns (companieId)
  [-] Removed foreign key on columns (userId)
  [+] Added foreign key on columns (companieId)
  [+] Added foreign key on columns (userId)

Prisma information

Environment & setup

  • OS: Mac OS
  • Database: MySQL
  • Node.js version: v12.18.3

Prisma Version

2.30.0

it works fine with version 2.27.0, 2.28.0, 2.29.0 but not 2.30.0

✔ Generated Prisma Client (2.27.0) to ./node_modules/@prisma/client in 1.95s
✔ Generated Prisma Client (2.28.0) to ./node_modules/@prisma/client in 1.98s
✔ Generated Prisma Client (2.29.0) to ./node_modules/@prisma/client in 1.99s

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
alan345commented, Sep 2, 2021

email sent to schemas@prisma.io

0reactions
pantharshit00commented, Sep 9, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Named constraints upgrade path - Prisma
After upgrading to Prisma 3, the default naming convention for constraint and index names will change and your primary and foreign key names...
Read more >
Data Modeling with Prisma - JavaScript in Plain English
In this case both models need to be updated. Optionally you can provide name to added foreign key constraint. Let's run the migration...
Read more >
How Prisma Introspects a Schema from a MongoDB Database
Prisma Migrate uses your changes to the Prisma schema to automatically ... schemas, or foreign keys to represent relations between tables.
Read more >
Hi I am getting a conflicting message after running prisma m | Orm ...
Hi I am getting a conflicting message after running prisma migrate dev on existing production database Changed the addresses table Removed foreign key...
Read more >
What is Prisma and Why Do We Need Another ORM?
Prisma takes the models written in the schema and with the prisma migrate command generates the SQL migrations as well as types that...
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