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)
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();
});
}
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?
Please or to participate in this conversation.