P
Prisma2mo ago
Postie

Migration adds creation of foreign key that already exists

On a MySql database there is a Download table that has this definition of a foreign key (removed columns for reading purposes):
create table Download
(
id int auto_increment primary key,
saleId int not null,
constraint Download_saleId_fkey
foreign key (saleId) references Sale (id)
on update cascade on delete cascade
);
create table Download
(
id int auto_increment primary key,
saleId int not null,
constraint Download_saleId_fkey
foreign key (saleId) references Sale (id)
on update cascade on delete cascade
);
This table already has a definition in the schema.prisma file and has been migrated earlier. So the foreign key is already defined and it exists. When I update the schema.prisma file with new models but no change in the definition of the Download model, the migration creates a line to add the foreign key to the Download table:
-- AddForeignKey
ALTER TABLE `Download` ADD CONSTRAINT `Download_saleId_fkey` FOREIGN KEY (`saleId`) REFERENCES `Sale`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `Download` ADD CONSTRAINT `Download_saleId_fkey` FOREIGN KEY (`saleId`) REFERENCES `Sale`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
So what I did was running it with --create-only , manually delete this line and then deploy the database. However, this could happen again in upcoming migrations and I was wondering if this can be fixed. So any ideas where I should look? Version used for Prisma "prisma": "6.18.0".
1 Reply
Prisma AI Help
Prisma AI Help2mo ago
You decided to hold for human wisdom. We'll chime in soon! Meanwhile, #ask-ai is there if you need a quick second opinion.

Did you find this page helpful?