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

semicolon24's avatar

error SQLSTATE[HY000]: General error: 1215

excuse me, I want to ask about error: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table activity_student add constraint activity_student_student_id_foreign foreign key (student_id) references students (id) on delete cascade on update cascade)

when I run the php artisan migrate:fresh command an error appears like that

please help with the solution The following is the coding of some of my tables:

  public function up()
   {
    Schema::create('contacts', function (Blueprint $table) {
        $table->id();
        $table->string('phone');
        $table->string('email');
        $table->string('address');
        $table->foreignId('student_id')->constrained('students')->cascadeOnUpdate()->cascadeOnDelete();
        $table->timestamps();
      });
   }


 public function up()
{
    Schema::create('students', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->integer('score');
        $table->foreignId('teacher_id')->constrained('teachers')->cascadeOnUpdate()->cascadeOnDelete();
        $table->timestamps();
    });
  }



public function up()
{
    Schema::create('activity_student', function (Blueprint $table) {
        $table->id();
        $table->foreignId('student_id')->constrained('students')->cascadeOnUpdate()->cascadeOnDelete();
        $table->foreignId('activity_id')->constrained('activities')->cascadeOnUpdate()->cascadeOnDelete();
        $table->timestamps();
    });
   }
0 likes
1 reply

Please or to participate in this conversation.