drush
drush copied to clipboard
sql-drop doesn't take foreign key into consideration.
drush @mysite sql-drop -y
Do you really want to drop all tables in the database MYDB? (y/n): y
ERROR 1217 (23000) at line 1: Cannot delete or update a parent row: a foreign key constraint fails
However, if we run it for another 2 times, it drops all of them.
Looking at the code, we get the list of tables by "SHOW TABLES" and pass them to DROP TABLES and the tables are trying to be dropped in random order.
Drupal has no support for foreign keys AFAIK. So we never worried about them. I would consider PRs on this.
I got the same error with a sql:drop
on a Server version: 10.5.12-MariaDB-0+deb11u1-log Debian 11
.
So I think we have to worry 😢
I found this stackoverflow : https://stackoverflow.com/a/63510059/1725936
I suggest this fix. I'll try to make a PR if nobody thinks it will introduce regressions: vendor/drush/drush/src/Sql/SqlBase.php:405
$sql = 'SET FOREIGN_KEY_CHECKS=0;';
$sql .= 'DROP TABLE '. implode(', ', $tables);
$sql .= 'SET FOREIGN_KEY_CHECKS=1;';