【问题标题】:Laravel Foreign key formed incorrectlyLaravel 外键格式错误
【发布时间】:2019-12-12 05:37:13
【问题描述】:

我在我的 laravel 应用程序上迁移表时遇到了一些问题。 它总是说

PDOException::("SQLSTATE[HY000]: 一般错误: 1005 无法创建表gamehosting.replies (errno: 150 "外键约束格式不正确")")

但是我没有看到外键有问题,这里是迁移

public function up()
    {
        Schema::create('replies', function (Blueprint $table) {
            $table->increments('id');
            $table->text('body');
            $table->integer('question_id')->unsigned();
            $table->integer('user_id');
            $table->foreign('question_id')->references('id')->on('questions')->onDelete('cascade');
            $table->timestamps();
        });
    }

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

顺便说一句,首先添加此表,然后应添加问题表,但错误会停止迁移。我将它设置为第一个迁移,但仍然存在同样的问题。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    这意味着questions 表上的id 不是无符号整数。 question_id 在数据库中的类型必须与questions 表上的id 相同。它可能失败的另一个原因是questions 表和replies 表没有相同的排序规则或相同的引擎。

    【讨论】:

      猜你喜欢
      • 2019-03-23
      • 2020-11-12
      • 1970-01-01
      • 2021-08-18
      • 2021-12-24
      • 1970-01-01
      • 2012-01-16
      • 2017-11-12
      • 2019-07-10
      相关资源
      最近更新 更多