I tried to create a migration for my tables, and everything seemed correct, but Laravel still refused to run the migration. The table order is also correct:
2025_10_27_134605_create_doctors_table.php
2025_10_27_134615_create_doctor_schedules_table.php
2025_10_27_134625_create_appointments_table.php
In the doctor_schedules table, I defined the schema as shown below.
doctor_schedules table
Schema::create('doctor_schedules', function (Blueprint $table) {
$table->id();
$table->date('available_date');
$table->time('available_time');
$table->unsignedBigInteger('doctor_id');
$table->boolean('is_booked')->default(false);
$table->foreign('doctor_id')->references('id')->on('doctors')->onDelete('cascade');
$table->timestamps();
});
In the appointments table, I referenced the doctor_schedules table using a foreign key.
Schema::create('appointments', function (Blueprint $table) {
$table->id();
$table->dateTime('appointment_date');
$table->unsignedBigInteger('patient_id');
$table->unsignedBigInteger('schedule_id');
$table->foreign('patient_id')->references('id')->on('patients')->onDelete('cascade');
$table->foreign('schedule_id')->references('id')->on('doctor_schedules')->onDelete('cascade');
$table->timestamps();
});
However, when I ran the migration, I received the following error:
SQLSTATE[HY000]: General error: 1005 Can't create table clinic_reservation.appointments (errno: 150 "Foreign key constraint is incorrectly formed") (Connection: mysql, SQL: alter table appointments add constraint appointments_schedule_id_foreign foreign key (schedule_id) references doctor_schedules (id) on delete cascade)