0

Master table POll

 public function up()
    {
        Schema::create('poll', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->string('poll_question', 255);
            $table->boolean('status',1)->default(0)->index();
            $table->unsignedInteger('order');
        });
    }

Detail table

  public function up()
    {
        Schema::create('poll_option', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->integer('poll_id');
            $table->string('poll_option', 255);
            $table->unsignedInteger('vote_poll_option');
            $table->unsignedInteger('order');
            $table->boolean('status',1)->default(0)->index();

            $table->foreign('poll_id')->references('id')->on('poll')->onUpdate('cascade')->onDelete('cascade');
        });
    }

When I run php artisan with foreign key

SQLSTATE[HY000]: General error: 1005 Can't create table mydb.#sql-6f4_433 (errno: 150 "Foreign key constraint is incorrectly formed")

Note: I m using laravel 5.2 and mysql type already Innodb What is main causes to incorrectly formed

rap-2-h
  • 30,204
  • 37
  • 167
  • 263
G-Rajendra
  • 215
  • 7
  • 17

1 Answers1

0

in Details table

public function up()
    {
        Schema::create('poll_option', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
            $table->integer('poll_id')->unsigned(); //Change Here
            $table->string('poll_option', 255);
            $table->unsignedInteger('vote_poll_option');
            $table->unsignedInteger('order');
            $table->boolean('status',1)->default(0)->index();

            $table->foreign('poll_id')
              ->references('id')->on('poll')
              ->onDelete('CASCADE')
              ->onUpdate('CASCADE');
        });
    }

this will work for you

Bindesh Pandya
  • 193
  • 3
  • 14