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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:50:43  来源:igfitidea点击:

Laravel foreign key onDelete('cascade') not working

sqllaraveleloquentforeign-key-relationship

提问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 表。我的迁移设置如下(简化):

userstable:

users桌子:

public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('email')->unique();
    });
}

rolestable:

roles桌子:

public function up()
{
    Schema::create('roles', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
    });
}

role_usertable:

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_usertable 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=InnoDBand 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 InnoDBin 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');