Laravel 外键 onDelete('cascade') 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24897300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel foreign key onDelete('cascade') not working
提问by Alias
I have a many-to-many relationship between User & Role, with a role_user table. My migrations are setup as so (simplified):
我有一个用户和角色之间的多对多关系,有一个 role_user 表。我的迁移设置如下(简化):
users
table:
users
桌子:
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('email')->unique();
});
}
roles
table:
roles
桌子:
public function up()
{
Schema::create('roles', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
});
}
role_user
table:
role_user
桌子:
public function up()
{
Schema::create('role_user', function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('role_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});
}
So as per the docs, I set my foreign keys to unsigned.
因此,根据文档,我将外键设置为未签名。
Now, I add a couple of users, and attach some roles - everything works fine. However, when I delete a user (User::destroy(2)
) the rows for that user in the role_user
table do not get deleted, which is causing redundant rows.
现在,我添加了几个用户,并附加了一些角色 - 一切正常。但是,当我删除用户 ( User::destroy(2)
) 时,role_user
表中该用户的行不会被删除,这会导致冗余行。
What am I doing wrong?
我究竟做错了什么?
- MySQL + InnoDB
- MySQL + InnoDB
EDIT: Grabbing the model and applying ->delete();
also has the same effect.
编辑:抓取模型并应用->delete();
也具有相同的效果。
回答by Jake
Try setting when trying to create this table. This fix has worked for me.
尝试创建此表时尝试设置。这个修复对我有用。
$table->engine = 'InnoDB';
I have filed a bug under: https://github.com/laravel/framework/issues/8730
我已经提交了一个错误:https: //github.com/laravel/framework/issues/8730
回答by STWilson
Check the MySQL config. my.inimay still have default-storage-engine=MYISAM
. Set to default-storage-engine=InnoDB
and you should avoid the trouble of adding this fix to each migration.
检查 MySQL 配置。my.ini可能仍然有default-storage-engine=MYISAM
. 设置为default-storage-engine=InnoDB
,您应该避免在每次迁移中添加此修复程序的麻烦。
回答by Shreyansh Panchal
It is established by Jake's answer that you have set default engine to InnoDB
Jake 的回答表明您已将默认引擎设置为 InnoDB
$table->engine = 'InnoDB';
Instead of doing it in each migration file, You can do so in config/database.php
而不是在每个迁移文件中这样做,你可以这样做 config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => 'InnoDB',
],
回答by Muaath Alhaddad
even after changing the engine to InnoDB
in Config->Database
it does not work
即使InnoDB
在 Config->Database中将引擎更改为它也不起作用
and the solution for that is to change the migration
解决方案是改变迁移
From
从
Schema::create('users', function (Blueprint $table) {
$table->BigIncrements('id');
Schema::create('users', function (Blueprint $table) {
$table->BigIncrements('id');
To
到
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
Schema::create('users', function (Blueprint $table) {
$table->increments('id');