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
{
// 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 { 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) {}
try {
DB::statement('ALTER TABLE peminjaman DROP FOREIGN KEY peminjaman_ibfk_1');
} 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
try { 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) {}
try {
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)
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
Schema::table('peminjaman', function (Blueprint $table) {
$table->foreign('id_user')
->references('id')
->on('users')
->onDelete('cascade');
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('id_buku')
->references('id_buku')
->on('buku')
->onDelete('cascade');
->references('id')
->on('bukus')
->onDelete('cascade');
});
}

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
{
// Only proceed if table exists (for backward compatibility)
if (!Schema::hasTable('buku_tamu')) {
return;
}
Schema::table('buku_tamu', function (Blueprint $table) {
// Drop old FK if exists
try {
@ -20,14 +25,20 @@ public function up(): void
// Ignore
}
// Change column type
DB::statement('ALTER TABLE buku_tamu MODIFY id_user BIGINT UNSIGNED NOT NULL');
try {
$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
$table->foreign('id_user')
->references('id')
->on('users')
->onDelete('cascade');
->references('id')
->on('users')
->onDelete('set null');
});
}

View File

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

View File

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

View File

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