laravel migration添加外键的最佳方法

res*_* kr 15 migration foreign-keys laravel

简单的问题:我是Laravel的新手.我有这个迁移文件:

Schema::create('lists', function(Blueprint $table) {
    $table->increments('id'); 
    $table->string('title', 255);
    $table->integer('user_id')->unsigned(); 
    $table->foreign('user_id')->references('id')->on('users'); 
    $table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)

我想更新它以添加onDelete('cascade').

最好的方法是什么?

Ner*_*nis 23

首先,你必须使你的user_id领域成为一个索引:

$table->index('user_id');
Run Code Online (Sandbox Code Playgroud)

之后,您可以使用级联动作创建外键:

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
Run Code Online (Sandbox Code Playgroud)

如果要通过新迁移执行此操作,则必须首先删除索引和外键,并从头开始执行所有操作.

在down()函数中你必须这样做然后在up()做我上面写的:

$table->dropForeign('lists_user_id_foreign');
$table->dropIndex('lists_user_id_index');
$table->dropColumn('user_id');
Run Code Online (Sandbox Code Playgroud)

  • 为什么不: `$table->dropForeign('user_id); $table->dropIndex('user_id');` ? (2认同)

RJF*_*res 14

从 Laravel 8 开始:

$table->foreignIdFor(OtherClass::class)->constrained();
Run Code Online (Sandbox Code Playgroud)

很简单 :)

  • 确保 OtherClass 迁移文件提前运行(像往常一样按文件名日期)。
  • 如果 OtherClass id 不是自动递增的,则 otherclass_id 的类型将是 char 而不是 bigint,在这种情况下->

使用这个代替:

$table->foreignId('otherclass_id')->index()->constrained()->cascadeOnDelete();
Run Code Online (Sandbox Code Playgroud)


小智 10

Schema::create('roles',function(Blueprint $table){

    $table->bigIncrements('id');
    $table->string('name');
    $table->timestamps();

});

Schema::create('permissions',function(Blueprint $table){

    $table->unsignedBigInteger('role_id');
    $table->foreign('role_id')->references('id')->on('roles');
    $table->string('permission');

});
Run Code Online (Sandbox Code Playgroud)


Roh*_*han 7

$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我们声明user_id列引用了users表上的id列。确保先创建外键列!该USER_ID列声明无符号,因为它不能有负值。

您还可以为约束的“删除时”和“更新时”操作指定选项:

$table->foreign('user_id')
      ->references('id')->on('users')
      ->onDelete('cascade');
Run Code Online (Sandbox Code Playgroud)

要删除外键,您可以使用dropForeign方法。外键使用与其他索引类似的命名约定:

$table->dropForeign('posts_user_id_foreign');
Run Code Online (Sandbox Code Playgroud)

如果你是相当新的Laravel口才,尝试了Laravel的划痕上laracasts提供系列。对于初学者来说,这是一个很好的指南。


PW_*_*ons 7

在 Laravel 7 中,它可以在一行中完成

$table->foreignId('user_id')->constrained()->cascadeOnDelete();


Tom*_*ler 6

如果您想添加onDelete('cascade')现有的外键,只需删除索引并重新创建它们:

public function up()
{
    Schema::table('lists', function($table)
    {
        $table->dropForeign('lists_user_id_foreign');

        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

public function down()
{
    Schema::table('lists', function($table)
    {
        $table->dropForeign('lists_user_id_foreign');

        $table->foreign('user_id')->references('id')->on('users');
    });
}
Run Code Online (Sandbox Code Playgroud)


小智 6

假设您有两个表 student 和 section ,您可以参考以下两个表结构来添加外键并制作 onDelete('cascade') 。

表格1 :

public function up()
{
    Schema::create('student', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('address');
        $table->string('phone');
        $table->string('about')->nullable();
        $table->timestamps();
    });
}
Run Code Online (Sandbox Code Playgroud)

表 - 2:

public function up()
{
    Schema::create('section', function (Blueprint $table) {
        $table->id();
        $table->bigInteger('student_id')->unsigned()->index()->nullable();
        $table->foreign('student_id')->references('id')->on('student')->onDelete('cascade');
        $table->string('section')->nullable();
        $table->string('stream')->nulable();
        $table->timestamps();
    });
}
Run Code Online (Sandbox Code Playgroud)

希望它会帮助你 - :) 你可以从这里阅读全文。