【问题标题】:Laravel SQLSTATE[23000] Cannot delete or update a parent row: a foreign key constraint failsLaravel SQLSTATE[23000] 无法删除或更新父行:外键约束失败
【发布时间】:2021-12-14 02:54:00
【问题描述】:
【问题讨论】:
标签:
php
mysql
laravel
foreign-keys
laravel-artisan
【解决方案1】:
我编写了一个简单的命令行来按顺序删除所有表。
代码:
Artisan::command('delete-all', function() {
(Schema::dropIfExists('bloggers'));
(Schema::dropIfExists('ticket_labels'));
(Schema::dropIfExists('labels'));
(Schema::dropIfExists('tickets'));
(Schema::dropIfExists('service_admins'));
(Schema::dropIfExists('services'));
(Schema::dropIfExists('admins'));
(Schema::dropIfExists('admin_roles'));
(Schema::dropIfExists('priorities'));
(Schema::dropIfExists('statuses'));
(Schema::dropIfExists('users'));
(Schema::dropIfExists('failed_jobs'));
(Schema::dropIfExists('migrations'));
(Schema::dropIfExists('password_resets'));
(Schema::dropIfExists('personal_access_tokens'));
// After deleting all the tables, now executing migrate command-line.
exec('php artisan migrate');
$this->comment('Done');
})->purpose('Deleting all the tables on database');
我希望你喜欢:)。
享受吧。
【解决方案2】:
问题可能是您正在尝试应用适用于旧版本 Laravel 的解决方案,如果您正在使用新版本,最好使用方括号来定义数组是什么。
public function down() {
Schema::table('users', function($table) {
$table->dropColumn(['description1', 'description2', 'description3']);
});
}
我建议你检查你正在使用的 laravel 的版本,并根据版本寻找答案。