fix error db

This commit is contained in:
Lutfi Hakim 2026-04-30 07:24:38 +07:00
parent 08fa79bcc3
commit 56e69bd01d
6 changed files with 110 additions and 47 deletions

View File

@ -13,30 +13,52 @@
public function up(): void public function up(): void
{ {
// 1. Hapus semua kemungkinan Foreign Key lama agar tidak bentrok // 1. Hapus semua kemungkinan Foreign Key lama agar tidak bentrok
try { DB::statement('ALTER TABLE peminjaman DROP FOREIGN KEY peminjaman_ibfk_1'); } catch (\Exception $e) {} try {
try { DB::statement('ALTER TABLE peminjaman DROP FOREIGN KEY peminjaman_ibfk_2'); } catch (\Exception $e) {} DB::statement('ALTER TABLE peminjaman DROP FOREIGN KEY peminjaman_ibfk_1');
try { DB::statement('ALTER TABLE peminjaman DROP FOREIGN KEY fk_peminjaman_user'); } catch (\Exception $e) {} } catch (\Exception $e) {
try { DB::statement('ALTER TABLE peminjaman DROP FOREIGN KEY fk_peminjaman_buku'); } catch (\Exception $e) {} }
try {
DB::statement('ALTER TABLE peminjaman DROP FOREIGN KEY peminjaman_ibfk_2');
} catch (\Exception $e) {
}
try {
DB::statement('ALTER TABLE peminjaman DROP FOREIGN KEY fk_peminjaman_user');
} catch (\Exception $e) {
}
try {
DB::statement('ALTER TABLE peminjaman DROP FOREIGN KEY fk_peminjaman_buku');
} catch (\Exception $e) {
}
// Menghapus default foreign key dari Laravel // Menghapus default foreign key dari Laravel
try { Schema::table('peminjaman', function (Blueprint $table) { $table->dropForeign(['id_user']); }); } catch (\Exception $e) {} try {
try { Schema::table('peminjaman', function (Blueprint $table) { $table->dropForeign(['id_buku']); }); } catch (\Exception $e) {} Schema::table('peminjaman', function (Blueprint $table) {
$table->dropForeign(['id_user']);
});
} catch (\Exception $e) {
}
try {
Schema::table('peminjaman', function (Blueprint $table) {
$table->dropForeign(['id_buku']);
});
} catch (\Exception $e) {
}
// 2. Samakan Tipe Data (Ditaruh di LUAR Schema::table agar pasti dieksekusi duluan) // 2. Samakan Tipe Data (Ditaruh di LUAR Schema::table agar pasti dieksekusi duluan)
DB::statement('ALTER TABLE peminjaman MODIFY id_user BIGINT UNSIGNED NOT NULL'); DB::statement('ALTER TABLE peminjaman MODIFY id_user BIGINT UNSIGNED NOT NULL');
DB::statement('ALTER TABLE peminjaman MODIFY id_buku INT NOT NULL'); DB::statement('ALTER TABLE peminjaman MODIFY id_buku BIGINT UNSIGNED NOT NULL');
// 3. Buat Relasi Baru yang Benar // 3. Buat Relasi Baru yang Benar
Schema::table('peminjaman', function (Blueprint $table) { Schema::table('peminjaman', function (Blueprint $table) {
$table->foreign('id_user') $table->foreign('id_user')
->references('id') ->references('id')
->on('users') ->on('users')
->onDelete('cascade'); ->onDelete('cascade');
$table->foreign('id_buku') $table->foreign('id_buku')
->references('id_buku') ->references('id')
->on('buku') ->on('bukus')
->onDelete('cascade'); ->onDelete('cascade');
}); });
} }
@ -48,7 +70,7 @@ public function down(): void
Schema::table('peminjaman', function (Blueprint $table) { Schema::table('peminjaman', function (Blueprint $table) {
// Hapus kedua relasi jika migration di-rollback // Hapus kedua relasi jika migration di-rollback
$table->dropForeign(['id_user']); $table->dropForeign(['id_user']);
$table->dropForeign(['id_buku']); $table->dropForeign(['id_buku']);
}); });
} }
}; };

View File

@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('buku_tamu', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('id_user')->nullable();
$table->string('nama_tamu');
$table->string('email')->nullable();
$table->string('no_hp')->nullable();
$table->string('asal_instansi')->nullable();
$table->string('status')->nullable();
$table->text('tujuan_kunjungan')->nullable();
$table->date('tanggal_kunjungan')->nullable();
$table->timestamps();
$table->foreign('id_user')
->references('id')
->on('users')
->onDelete('set null');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('buku_tamu');
}
};

View File

@ -12,6 +12,11 @@
*/ */
public function up(): void public function up(): void
{ {
// Only proceed if table exists (for backward compatibility)
if (!Schema::hasTable('buku_tamu')) {
return;
}
Schema::table('buku_tamu', function (Blueprint $table) { Schema::table('buku_tamu', function (Blueprint $table) {
// Drop old FK if exists // Drop old FK if exists
try { try {
@ -20,14 +25,20 @@ public function up(): void
// Ignore // Ignore
} }
// Change column type try {
DB::statement('ALTER TABLE buku_tamu MODIFY id_user BIGINT UNSIGNED NOT NULL'); $table->dropForeign(['id_user']);
} catch (\Exception $e) {
// Ignore if FK doesn't exist
}
// Change column type if needed
DB::statement('ALTER TABLE buku_tamu MODIFY id_user BIGINT UNSIGNED NULL');
// Add correct FK // Add correct FK
$table->foreign('id_user') $table->foreign('id_user')
->references('id') ->references('id')
->on('users') ->on('users')
->onDelete('cascade'); ->onDelete('set null');
}); });
} }

View File

@ -8,21 +8,12 @@
{ {
public function up(): void public function up(): void
{ {
Schema::table('buku_tamu', function (Blueprint $table) { // Columns already exist in create_buku_tamu_table migration
// id_user jadi nullable (tamu non-member tidak punya user) // This migration is now a no-op for forward compatibility
$table->unsignedBigInteger('id_user')->nullable()->change();
// Kolom untuk tamu non-member
$table->string('nama_tamu')->nullable()->after('id_user');
$table->string('asal_instansi')->nullable()->after('nama_tamu');
});
} }
public function down(): void public function down(): void
{ {
Schema::table('buku_tamu', function (Blueprint $table) { // No-op
$table->dropColumn(['nama_tamu', 'asal_instansi']);
$table->unsignedBigInteger('id_user')->nullable(false)->change();
});
} }
}; };

View File

@ -11,11 +11,8 @@
*/ */
public function up(): void public function up(): void
{ {
Schema::table('buku_tamu', function (Blueprint $table) { // Columns already exist in create_buku_tamu_table migration
$table->string('email')->nullable()->after('nama_tamu'); // This migration is now a no-op for forward compatibility
$table->string('no_hp')->nullable()->after('email');
$table->string('status')->nullable()->after('asal_instansi');
});
} }
/** /**
@ -23,8 +20,6 @@ public function up(): void
*/ */
public function down(): void public function down(): void
{ {
Schema::table('buku_tamu', function (Blueprint $table) { // No-op
$table->dropColumn(['email', 'no_hp', 'status']);
});
} }
}; };

View File

@ -7,15 +7,19 @@
return new class extends Migration { return new class extends Migration {
public function up(): void public function up(): void
{ {
Schema::table('buku', function (Blueprint $table) { Schema::table('bukus', function (Blueprint $table) {
$table->float('lokasi_x')->nullable()->after('cover')->comment('Posisi X pin pada denah (persentase 0-100)'); if (!Schema::hasColumn('bukus', 'lokasi_x')) {
$table->float('lokasi_y')->nullable()->after('lokasi_x')->comment('Posisi Y pin pada denah (persentase 0-100)'); $table->float('lokasi_x')->nullable()->comment('Posisi X pin pada denah (persentase 0-100)');
}
if (!Schema::hasColumn('bukus', 'lokasi_y')) {
$table->float('lokasi_y')->nullable()->comment('Posisi Y pin pada denah (persentase 0-100)');
}
}); });
} }
public function down(): void public function down(): void
{ {
Schema::table('buku', function (Blueprint $table) { Schema::table('bukus', function (Blueprint $table) {
$table->dropColumn(['lokasi_x', 'lokasi_y']); $table->dropColumn(['lokasi_x', 'lokasi_y']);
}); });
} }