mona_salih liked a comment+100 XP
5mos ago
mona_salih wrote a reply+100 XP
5mos ago
mona_salih wrote a reply+100 XP
5mos ago
mona_salih wrote a reply+100 XP
5mos ago
I'm using laravel12
I checked the database, and it didn’t create the appointments table or the doctor_schedules table. The column schedule_id in the appointments table is a foreign key that references the id column in the doctor_schedules table.
appointments table
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(); });
doctor_schedules table Schema::create('doctor_schedules', function (Blueprint $table) { $table->engine = 'InnoDB'; $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(); });
mona_salih started a new conversation+100 XP
5mos ago
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)