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

TikTina's avatar

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table therapies add constraint therapies_therapy_id_foreign foreign key (therapy_id) references therapy (id))

I have three main tables in migrations: therapies, symptoms and herb, and two pivot tables: therapy_symptom and herb_therapy.

I don't know what is the problem here?

0 likes
5 replies
Sinnbeck's avatar

Can you show the migration that fails? My best guess is that therapy_idisn't a big integer

TikTina's avatar

@Sinnbeck

   Schema::create('therapies', function (Blueprint $table) {
            $table->id();
            $table->text('therapy');
            $table->timestamps();


        });
    }

 {
        Schema::create('symptom_therapy', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('symptom_id');
            $table->foreign('symptom_id')->references('id')->on('symptom');
            $table->unsignedBigInteger('therapy_id');
            $table->foreign('therapy_id')->references('id')->on('therapy');
            $table->timestamps();
        });
    }

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@TikTina the table is called therapies but you reference on('therapy')

$table->foreign('therapy_id')->references('id')->on('therapies'); 
1 like

Please or to participate in this conversation.