mona_salih's avatar

Error in db migration

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)

0 likes
8 replies
JussiMannisto's avatar

Laravel isn't refusing to run the migration. It's trying to run the migration but the database returns an error because the foreign key is incorrectly formed: either the doctor_schedules table doesn't exist, or its id doesn't match the type of the foreign key column.

It looks to me like the columns are defined correctly in the migrations, so I would guess something has gone wrong. Was there an issue with an earlier migration attempt? Check the database to see if the doctor_schedules table was created correctly, and its id is an unsigned bigint.

Btw, which Laravel version are you running?

mona_salih's avatar

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(); });

JussiMannisto's avatar

That's the problem then. The doctor_schedules table must exist before the appointments table is created.

Let's try to figure out why the doctor_schedules migration isn't running. Run php artisan migrate:status and show the ouput. Also show the full migration file, not just the column definitions.

mona_salih's avatar

It works now. I updated the migration file dates, and everything is working perfectly.

JussiMannisto's avatar
Level 50

The order of your migration files is wrong. You're trying to create the appointments table before the doctor_schedules table, which won't work.

In your original post you showed these migration filenames:

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

But that's not what you have in your project. Where did these filenames come from?

P.S. Migration files are sorted lexiographically. That means 2025_10_27 comes before 2025_10_3. If you manually edit the filenames, you need to zero-pad the dates, e.g. 2025_10_03.

mona_salih's avatar

It works now. I updated the migration file dates, and everything is working perfectly. yes It work many thanks I totly forget stauts command and it work

JussiMannisto's avatar

Glad to hear. You can mark the thread as solved by selecting a best answer.

1 like

Please or to participate in this conversation.