drush icon indicating copy to clipboard operation
drush copied to clipboard

sql-drop doesn't take foreign key into consideration.

Open vijaycs85 opened this issue 9 years ago • 2 comments

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.

vijaycs85 avatar Dec 16 '15 09:12 vijaycs85

Drupal has no support for foreign keys AFAIK. So we never worried about them. I would consider PRs on this.

weitzman avatar Dec 19 '15 02:12 weitzman

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;';

obriat avatar Mar 22 '22 13:03 obriat