r/SQL • u/Nyrien_nml • May 23 '26
MariaDB HeidiSQL Migration Error
Hi! I'm self-taught in programming and have never learned how to handle databases; I'm trying to run a FiveM server from this opensource code: https://github.com/SOZ-Faut-etre-Sub/SOZ-FiveM-Server and followed their explanations to migrate the database, but I'm running into an error from the command: yarn run prisma migrate deploy and can't really figure out why.
This is what's in the migration.sql :
-- DropForeignKey
ALTER TABLE `race_score` DROP FOREIGN KEY `race_score_ibfk_1`;
-- DropForeignKey
ALTER TABLE `race_score` DROP FOREIGN KEY `race_score_ibfk_2`;
-- AlterTable
ALTER TABLE `vandalism_props` MODIFY `location` TEXT NOT NULL;
-- AlterTable
ALTER TABLE `vehicles` MODIFY `maxStock` INTEGER NULL DEFAULT 2;
-- CreateTable
CREATE TABLE `zone` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`type` ENUM('NoStress') NOT NULL DEFAULT 'NoStress',
`zone` LONGTEXT NOT NULL,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- CreateIndex
CREATE INDEX `name` ON `race`(`name`);
This is the error I get in the cmd:
Applying migration `20231001162840_add_zone`
Error: P3018
A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
Migration name: 20231001162840_add_zone
Database error code: 1091
Database error:
Can't DROP FOREIGN KEY `race_score_ibfk_1`; check that it exists
Please check the query number 1 from the migration file.
error Command failed with exit code 1.
And here is what I have on HeidiSQL

Can you explain to me what's happening as if I'm 10 and have absolutely no idea what those terms mean? :)
1
u/Aggressive_Ad_5454 May 26 '26
Your migration script makes assumptions about the current state of your database. In particular it assumes that your
race_scoretable already contains a foreign key namedrace_score_ibfk_1.But your table does not already contain that foreign key. So that migration script isn’t suitable for your database as written.
Without knowing the current state of your database and also the desired state after the migration it is hard to give you advice on how to fix this.
Database migrations are a notorious pain in the ass.
1
u/navicat-gu 28d ago
ELI10 version: a migration is a list of "change the database from state A to state B" steps. Prisma runs them in order and assumes your database is exactly in state A. The very first step in this one says "remove the link called
race_score_ibfk_1", but your copy ofrace_scoredoesn't have a link with that name. Maybe it was never created, or it got created with a different name. MySQL can't remove something that isn't there, so it stops (error 1091). Prisma then marks the migration as failed (P3018) and refuses to run anything after it until you deal with it.Since you said the database is empty, the easiest fix is to start clean and let Prisma build everything from scratch:
That drops the database, recreates it and applies every migration in order. Because each step then runs on the state it expects, the foreign key will exist when
add_zonetries to drop it. Only do this when there's no data you care about.If you'd rather keep what's there:
SHOW CREATE TABLE race_score;(HeidiSQL also lists them on the table's "Foreign keys" tab).race_score_ibfk_1/_2aren't there, those two DROP lines have nothing to do. Run the rest ofmigration.sqlby hand in HeidiSQL's query tab, starting from the-- AlterTableline.yarn run prisma migrate resolve --applied 20231001162840_add_zoneyarn run prisma migrate deployagain for any later migrations.The usual cause is importing a SQL dump or creating tables by hand before running migrations. Then the database is no longer in the state the migration history expects. With Prisma, it's best to let
migrate deploy(orreset) create everything.Disclosure: I'm RC, Director of Digital Strategy @ Navicat. HeidiSQL is fine for this. Any GUI just lets you look at the tables and keys while Prisma does the real work. If you ever want another client, Navicat 18 does the same job and Navicat Premium Lite is free.