用户登录
用户注册

分享至

Laravel :: 更新外键的最佳方式

  • 作者: 优酷土豆乐视
  • 来源: 51数据库
  • 2022-10-21

问题描述

我有这个迁移文件

Schema::create('table_one', function(Blueprint $table) 
{ 
    $table->increments('id'); 
    $table->string('name'); 
    $table->integer('table_two_id')->unsigned(); 
    $table->foreign('table_two_id')->references('id')->on('table_two'); 
    $table->timestamps(); 
});

我想更新以使其 ->onDelete('cascade');

$table->foreign('table_two_id')->references('id')->on('table_two')->onDelete('cascade');

最好的方法是什么?

有没有类似->change();

谢谢

推荐答案

删除外键然后再次添加并运行迁移.

Drop the foreign key then add it again and run migrate.

public function up()
{
    Schema::table('table_one', function (Blueprint $table) {
        $table->dropForeign(['table_two_id']);

        $table->foreign('table_two_id')
            ->references('id')
            ->on('table_two')
            ->onDelete('cascade');
    });
}
软件
前端设计
程序设计
Java相关