0

主表 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');
        });
    }

明细表

  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');
        });
    }

当我用外键运行 php artisan

SQLSTATE[HY000]:一般错误:1005 无法创建表mydb#sql-6f4_433(errno: 150 "外键约束格式不正确")

注意:我正在使用laravel 5.2和 mysql 类型已经 Innodb 什么是错误形成的主要原因

4

1 回答 1

0

在详细信息表中

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');
        });
    }

这对你有用

于 2016-07-21T14:36:53.950 回答