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

Posted 3 years ago by netcore

Hello. This is my code.

class CreateHelpParentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('help_parents', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('shortname');
            $table->integer('position');
            $table->timestamps();

            $table->foreign('id')
                ->references('parent')->on('help')
                ->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('help_parents');
    }
}

class CreateHelpTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('help', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('shortname');
            $table->integer('parent');
            $table->text('content');
            $table->integer('position');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('help');
    }
}

When I run php artisan migrate I get this:

  [Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `help_parents` add constraint help_parents_id_foreign foreign key (`id`) references `help` (`parent`) on delete cascade)






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

Please help

Please sign in or create an account to participate in this conversation.