Migrate dev - drift detected on foreign keys
See original GitHub issueBug description
When i switched from mysql 8 to mariadb 10.7 migrate dev stopped working properly. On mysql all my migrations run as expected, but on mariadb i end up with infinite new migration file generating where all my foreign keys are recreated again. Every time i run migrate dev
it creates identical migration or ends up with “Drift detected” error.
Previusly tested on Prisma 3.0.2. Upgaded to 3.8.1 after error occured. Same problem on new version.
Here detected drift
[*] Changed the `UnitHasUserLogin` table
[-] Removed foreign key on columns (serialNumber)
[-] Removed foreign key on columns (userId)
[+] Added foreign key on columns (serialNumber)
[+] Added foreign key on columns (userId)
[*] Changed the `UnitPayload` table
[-] Removed foreign key on columns (serialNumber)
[+] Added foreign key on columns (serialNumber)
[*] Changed the `UserToken` table
[-] Removed foreign key on columns (userId)
[+] Added foreign key on columns (userId)
Here generated migration
-- DropForeignKey
ALTER TABLE `UnitHasUserLogin` DROP FOREIGN KEY `UnitHasUserLogin_serialNumber_fkey`;
-- DropForeignKey
ALTER TABLE `UnitHasUserLogin` DROP FOREIGN KEY `UnitHasUserLogin_userId_fkey`;
-- DropForeignKey
ALTER TABLE `UnitPayload` DROP FOREIGN KEY `UnitPayload_serialNumber_fkey`;
-- DropForeignKey
ALTER TABLE `UserToken` DROP FOREIGN KEY `UserToken_userId_fkey`;
-- AddForeignKey
ALTER TABLE `UserToken` ADD CONSTRAINT `UserToken_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `UserLogin`(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
-- AddForeignKey
ALTER TABLE `UnitHasUserLogin` ADD CONSTRAINT `UnitHasUserLogin_serialNumber_fkey` FOREIGN KEY (`serialNumber`) REFERENCES `Unit`(`serialNumber`) ON DELETE CASCADE ON UPDATE RESTRICT;
-- AddForeignKey
ALTER TABLE `UnitHasUserLogin` ADD CONSTRAINT `UnitHasUserLogin_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `UserLogin`(`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
-- AddForeignKey
ALTER TABLE `UnitPayload` ADD CONSTRAINT `UnitPayload_serialNumber_fkey` FOREIGN KEY (`serialNumber`) REFERENCES `Unit`(`serialNumber`) ON DELETE CASCADE ON UPDATE RESTRICT;
How to reproduce
Just run migrate dev on mysql or mariadb fresh container.
Expected behavior
I expect same behavior on both databases.
Prisma information
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model UserLogin {
id String @id @default(uuid()) @db.Char(36)
email String @unique @db.VarChar(64)
salt String @db.Char(16)
pwd String @db.Char(128)
dateCreated DateTime @default(now())
role UserRole @default(User)
note String? @db.Text
active Boolean @default(false)
language Languages @default(en)
units UnitHasUserLogin[]
tokens UserToken[]
}
model UserToken {
id String @id @db.Char(128)
userId String @db.Char(36)
type TokenType
dateCreated DateTime @default(now())
dateValidTo DateTime?
audience String? @db.VarChar(32)
userAgent String? @db.VarChar(255)
payload String? @unique @db.VarChar(255)
language String? @db.VarChar(32)
user UserLogin @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: Restrict)
@@index([userId, type], map: "userId_type_UserToken")
}
model UnitHasUserLogin {
serialNumber String @db.VarChar(12)
userId String @db.Char(36)
dateCreated DateTime @default(now())
unit Unit @relation(fields: [serialNumber], references: [serialNumber], onDelete: Cascade, onUpdate: Restrict)
user UserLogin @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: Restrict)
@@id([serialNumber, userId])
@@index([userId], map: "UnitHasUserLogin_userId_fkey")
}
model Unit {
serialNumber String @id @db.VarChar(12)
dateCreated DateTime @default(now())
automat String @db.VarChar(12)
blocked Boolean @default(false)
note String? @db.Text
users UnitHasUserLogin[]
unitPayloads UnitPayload[]
}
model UnitPayload {
dateCreated DateTime @default(now())
serialNumber String @db.VarChar(12)
direction Direction
id Int @id @default(autoincrement()) @db.UnsignedInt
payload Bytes @db.TinyBlob
unit Unit @relation(fields: [serialNumber], references: [serialNumber], onDelete: Cascade, onUpdate: Restrict)
@@index([dateCreated], map: "dateCreated")
@@index([serialNumber], map: "serialNumber")
// index in db is sorted DESC, prisma does not allow sort order here
@@index([serialNumber, dateCreated], map: "serialNumber_dateCreated")
}
enum Languages {
en
de
cs
}
enum TokenType {
AUTH
FPWD
}
enum Direction {
Tx
Rx
}
enum UserRole {
User
Admin
Root
}
Environment & setup
- OS: Ubuntu 21.04
- Database: mysql 8, mariadb 10.7
- Node.js version: 16.3
Prisma Version
prisma : 3.8.1
@prisma/client : 3.8.1
Current platform : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at ../../node_modules/.pnpm/@prisma+engines@3.8.0-43.34df67547cf5598f5a6cd3eb45f14ee70c3fb86f/node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine : migration-engine-cli 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at ../../node_modules/.pnpm/@prisma+engines@3.8.0-43.34df67547cf5598f5a6cd3eb45f14ee70c3fb86f/node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine : introspection-core 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at ../../node_modules/.pnpm/@prisma+engines@3.8.0-43.34df67547cf5598f5a6cd3eb45f14ee70c3fb86f/node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary : prisma-fmt 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f (at ../../node_modules/.pnpm/@prisma+engines@3.8.0-43.34df67547cf5598f5a6cd3eb45f14ee70c3fb86f/node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash : 34df67547cf5598f5a6cd3eb45f14ee70c3fb86f
Studio : 0.452.0
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:10 (3 by maintainers)
Top Results From Across the Web
Migrations - Drift - Simon Binder
Drift provides a migration API that can be used to gradually apply schema changes after bumping the schemaVersion getter inside the Database class....
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 >Migration troubleshooting in development - Prisma
If Prisma Migrate detects a migration history conflict when you run prisma migrate dev , the CLI will ask to reset the database...
Read more >Dealing with Database Data and Metadata in Flyway ...
How to detect database drift prior to running a database migration, so that you can be certain that a database hasn't been subject...
Read more >Why isn't referentialIntegrity = "prisma" preventing foreign keys ...
npx prisma migrate dev ... Database error code: 1317 Database error: Foreign keys cannot be created on this database.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In my case, I found the problem origin: By default the mysql tables were created with MyISAM engine, I changed the default engine to InnoDB, MyISAM does not support relatioships.
It possible to help devs warning them, I leave two suggestions:
Maybe MySQL table engines have relation with the original issue reported by @hrasekj
I have the same issue here, MariaDB. prisma is repeating foreing keys in every migration.
– AddForeignKey ALTER TABLE
post
ADD CONSTRAINTpost_userFK
FOREIGN KEY (userId
) REFERENCESuser
(id
) ON DELETE NO ACTION ON UPDATE NO ACTION;– AddForeignKey ALTER TABLE
post_comment
ADD CONSTRAINTpost_comment_userFK
FOREIGN KEY (userId
) REFERENCESuser
(id
) ON DELETE NO ACTION ON UPDATE NO ACTION;