Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mike_3096's avatar

General error: 1215 Cannot add foreign key constraint

I'm trying to create student and alumni table simultaneously where s_id and a_id are the foreign keys that references sid and aid from respective tables but i'm getting the errors on alumni table. Error:" alter table alumni add constraint alumni_s_id_foreign foreign key (s_id) references students (sid) on delete cascade)"

Alumni table

 public function up()
{
    Schema::create('alumni', function (Blueprint $table) {
        $table->increments('aid');
        $table->integer('slot_id');
        $table->time('s_time');
        $table->time('e_time');
        $table->boolean('is_booked');
        $table->unsignedInteger('s_id');
        $table->foreign('s_id')->references('sid')->on('students')->onDelete('cascade');
        $table->timestamps();
    });
}

Student table

 public function up()
{
    Schema::create('students', function (Blueprint $table) {
        $table->increments('sid');
        $table->unsignedInteger('a_id');
        $table->foreign('a_id')->references('aid')->on('alumni')->onDelete('cascade');
        $table->time('slot');
        $table->timestamp('date');
        $table->boolean('status');
        $table->timestamps();
    });
}
0 likes
11 replies
D9705996's avatar

Your column types in both tables need to be identical, including any indexes. Try adding $table->index('a_id'); or I think you can chain ->index()to your unsignedintegers before you create your foreign key (repeat for the other migrations)

D9705996's avatar
D9705996
Best Answer
Level 51

Just noticed that you have recursive foreign keys which is probably the main issue. You might need a third migration just to apply the foreign key constraints as it will ensure both tables exist. Also do you really want the alumni to be deleted when you delete the student?

Mike_3096's avatar

Yas... its required .... I'll trying creating another migration

Snapey's avatar

Whichever migration you run first, the other table will not exist. Create a seperate migration or run all three together. There is nothing stopping you from migrating all in one file;

 public function up()
{
    Schema::create('alumni', function (Blueprint $table) {
        $table->increments('aid');
        $table->integer('slot_id');
        $table->time('s_time');
        $table->time('e_time');
        $table->boolean('is_booked');
        $table->unsignedInteger('s_id');
        $table->timestamps();
    });

    Schema::create('students', function (Blueprint $table) {
        $table->increments('sid');
        $table->unsignedInteger('a_id');
        $table->time('slot');
        $table->timestamp('date');
        $table->boolean('status');
        $table->timestamps();
    });

    Schema::table('students', function (Blueprint $table) {
        $table->foreign('a_id')->references('aid')->on('alumni')->onDelete('cascade');
    }

    Schema::table('alumni', function (Blueprint $table) {
        $table->foreign('s_id')->references('sid')->on('students')->onDelete('cascade');
    }


}

}

Mike_3096's avatar

@D9705996 - Sure... there is one more thing i'm stuck with how can i enter data in such table as either of the table cannot be filled due to recursive foreign key because they contain their respective primary keys in each other..

Mike_3096's avatar

@D9705996 - ya actually ....... just went the documentation ...... finally sorted things.... Thanks again

1 like
D9705996's avatar

Welcome. Give us a shout if you need a hand

Please or to participate in this conversation.