簡體   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

首先,我的英語很差。 對於那個很抱歉。

我在cards表中有一個外鍵,例如:

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

由於某些原因,我需要更改cascade以在該列上set null

我嘗試過(在新的遷移中):

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

運行正常,但刪除時未設置 null:((

我該如何解決。 謝謝!!

您必須在新遷移中更改表遷移架構,並確保將外鍵字段設置為可為空:

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

然后像這樣使用set null

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

如果要首先更改FOREIGN KEY約束,則需要刪除外鍵的先前索引並僅將外鍵約束重新添加到列。

嘗試這個:

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();

        });

你不能只修改現有的遷移,你需要再做一個

<?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()
    {
          
    }
}

經過多次收集和嘗試,我找到了最好的解決方案(不需要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