简体   繁体   English

如何将 mysql 8 中的约束 FOREIGN KEY 从 laravel 迁移或原始 ZAC5C74B64B4B8352EF2F181AFFB5AC 中的“ON DELETE CASCADE”更改为“ON DELETE SET NULL”

[英]How to change constraint FOREIGN KEY in mysql 8 from `ON DELETE CASCADE` to `ON DELETE SET NULL` in laravel migration or raw sql

Firstly, my English very bad.首先,我的英语很差。 Sorry about that.对于那个很抱歉。

I have a foreign key in cards table like:我在cards表中有一个外键,例如:

$table->foreignId('wallet_id')->constrained('wallets', 'id')->onDelete('cascade');

Since some reasons I need change cascade to set null on that column由于某些原因,我需要更改cascade以在该列上set null

I have try (in a new migration):我尝试过(在新的迁移中):

$table->dropForeign('cards_wallet_id_foreign');
$table->foreignId('wallet_id')
    ->nullable()
    ->onDelete('set null')
    ->change();

that run okey but when delete it not set null:((运行正常,但删除时未设置 null:((

How I can solve that.我该如何解决。 Thank you!!谢谢!!

You must change your table migration schema in a new migration, and make sure you set the foreign key field as nullable:您必须在新迁移中更改表迁移架构,并确保将外键字段设置为可为空:

$table->integer('wallet_id')->unsigned()->nullable();

and then use set null like this:然后像这样使用set null

$table->...->onDelete('set null');

If you want to make changes to FOREIGN KEY Constraints first you need to drop the previous index of the foreign key and re add only Forign key Constraints to column.如果要首先更改FOREIGN KEY约束,则需要删除外键的先前索引并仅将外键约束重新添加到列。

Try this:尝试这个:

Schema::table('cards', function (Blueprint $table) {
            //Drop Previous Index
            $table->dropForeign('cards_wallet_id_foreign');

            //Since we want to set null on Delete Or Update
            $table->unsignedBigInteger('wallet_id')->nullable()->change();

            //Adding Only Forign key Constraints to column
            //calling foreignId will re attempt to create a column
            $table->foreign('wallet_id')->references('id')->on('wallets')->onDelete('set null')->onUpdate('set null')->change();

        });

You can not just modify the existing migration, You need to make another one你不能只修改现有的迁移,你需要再做一个

<?php
  
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
  
class ChangeSomeTableColumn extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('the_table_you_are_changing', function (Blueprint $table) {
            $table
                ->foreignId('wallet_id')
                ->constrained('wallets', 'id')
                ->onDelete('set null')
                ->change();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
          
    }
}

After collected and try many times I was found the best solution (don't need drop column)经过多次收集和尝试,我找到了最好的解决方案(不需要drop column)

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AlterWalletIdColumnInCardsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('cards', function (Blueprint $table) {
            $table->dropForeign('cards_wallet_id_foreign');
            $table->foreignId('wallet_id')
                ->nullable()
                ->change();

            $table->foreign('wallet_id')
                ->on('wallets')
                ->references('id')
                ->onDelete('set null');
        });
    }

    /**
     * I recommend you don't use down (laravel 9 will remove it as default 
     * migration)
     */
    public function down()
    {
        Schema::table('cards', function (Blueprint $table) {
            $table->dropForeign('cards_wallet_id_foreign');
            $table->foreignId('wallet_id')
                ->nullable(false)
                ->change();

            $table->foreign('wallet_id')
                ->on('wallets')
                ->references('id')
                ->onDelete('cascade');
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM