-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Describe the Bug
When running "migrate drop" on a Mysql/MariaDB database that has a foreign key constraint between two tables, the action will fail if the table referenced by the constraint is dropped first..
While MySQL/MariaDB accepts CASCADE
as an option to DROP TABLE
, it is not actually implemented and does nothing.
Steps to Reproduce
- Have a MySQL/MariaDB database with two tables, having the alphabetically last one depend on the former.
CREATE TABLE `a` (
`id` int(11) NOT NULL,
PRIMARY KEY (`id`) USING BTREE
);
CREATE TABLE `b` (
`id` int(11) NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
CONSTRAINT `b_id_a_id` FOREIGN KEY (`id`) REFERENCES `a` (`id`)
);
- Run
migration drop
on this database - See error;
Cannot delete or update a parent row: a foreign key constraint fails in line 0: DROP TABLE IF EXISTS
a
CASCADE
Expected Behavior
I would expect both tables to be dropped as would happen if CASCADE
was actually implemented.
Migrate Version
4.2.5
Loaded Source Drivers
gcs, file, s3, github, go-bindata, godoc-vfs
Loaded Database Drivers
cockroach, cockroachdb, postgresql, spanner, stub, cassandra, clickhouse, mysql, postgres, redshift, crdb-postgres, mongodb
Go Version
go version go1.12 linux/amd64
Additional context
MySQL docs on DROP TABLE;
The RESTRICT and CASCADE keywords do nothing. They are permitted to make porting easier from other database systems.
RESTRICT and CASCADE are allowed to make porting from other database systems easier. In MariaDB, they do nothing.
In MySQL/MariaDB, instead of using CASCADE
, the checking for constraints can be (temporarily) disabled by changing the system variable foreign_key_checks
;
https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_foreign_key_checks
This issue is fixable by executing a SET FOREIGN_KEY_CHECKS = 0
before attempting to drop tables, though that would introduce a new bug allowing to delete tables which are referenced by views. This would be fixed if views would also be deleted, which currently are not, but should.