【问题标题】:php artisan migrate foreign key errorphp artisan 迁移外键错误
【发布时间】:2016-11-25 00:10:51
【问题描述】:

主表 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 不正确形成的主要原因是什么

【问题讨论】:

  • 五月 $table->integer('poll_id')->unsigned();
  • 或$table->unsignedInteger('poll_id')

标签: php mysql laravel foreign-keys artisan-migrate


【解决方案1】:

在详细信息表中

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

这对你有用

【讨论】:

  • 谢谢宾德什。我错过了外键字段中的“->unsigned()”。
猜你喜欢
  • 2020-03-17
  • 1970-01-01
  • 2017-09-27
  • 2021-08-21
  • 2014-10-05
  • 2015-03-29
  • 2017-06-21
  • 2020-01-05
  • 2020-06-28
相关资源
最近更新 更多