ipdate
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
// 1. Modify columns to match referenced types (unsignedBigInteger) and make them nullable
|
||||
Schema::table('order', function (Blueprint $table) {
|
||||
$table->unsignedBigInteger('customer_id')->nullable()->default(null)->change();
|
||||
$table->unsignedBigInteger('user_id')->nullable()->default(null)->change();
|
||||
$table->unsignedBigInteger('discount_id')->nullable()->default(null)->change();
|
||||
$table->unsignedBigInteger('invoice_id')->nullable()->default(null)->change();
|
||||
});
|
||||
|
||||
// 2. Convert default 0 values to null to ensure foreign key integrity
|
||||
DB::table('order')->where('customer_id', 0)->update(['customer_id' => null]);
|
||||
DB::table('order')->where('user_id', 0)->update(['user_id' => null]);
|
||||
DB::table('order')->where('discount_id', 0)->update(['discount_id' => null]);
|
||||
DB::table('order')->where('invoice_id', 0)->update(['invoice_id' => null]);
|
||||
|
||||
// 3. Add foreign key constraints
|
||||
Schema::table('order', function (Blueprint $table) {
|
||||
$table->foreign('customer_id')->references('id')->on('customer')->onDelete('set null');
|
||||
$table->foreign('user_id')->references('id')->on('user')->onDelete('set null');
|
||||
$table->foreign('discount_id')->references('id')->on('discount')->onDelete('set null');
|
||||
$table->foreign('invoice_id')->references('id')->on('invoice')->onDelete('set null');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('order', function (Blueprint $table) {
|
||||
// Drop foreign keys
|
||||
$table->dropForeign(['customer_id']);
|
||||
$table->dropForeign(['user_id']);
|
||||
$table->dropForeign(['discount_id']);
|
||||
$table->dropForeign(['invoice_id']);
|
||||
|
||||
// Revert columns to previous state (integers/bigInteger with default 0)
|
||||
// Note: This matches the original migration definitions
|
||||
$table->bigInteger('customer_id')->default(0)->nullable(false)->change();
|
||||
$table->integer('user_id')->default(0)->nullable(false)->change();
|
||||
$table->integer('discount_id')->default(0)->nullable(false)->change();
|
||||
$table->integer('invoice_id')->default(0)->nullable(false)->change();
|
||||
});
|
||||
|
||||
// Restore 0 values where null (optional, but keeps consistency with down())
|
||||
DB::table('order')->whereNull('customer_id')->update(['customer_id' => 0]);
|
||||
DB::table('order')->whereNull('user_id')->update(['user_id' => 0]);
|
||||
DB::table('order')->whereNull('discount_id')->update(['discount_id' => 0]);
|
||||
DB::table('order')->whereNull('invoice_id')->update(['invoice_id' => 0]);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user