PDOException::(“” SQLSTATE [HY000]:(错误号:150“外键约束格式不正确”)“)

时间:2020-02-25 08:11:31

标签: php laravel-6

我有此错误:

PDOException::(“ SQLSTATE [HY000]:常规错误:1005无法创建表shreemadposts(错误号:150”外键约束格式不正确“)”)

表1:

public function up()
{
    Schema::create('category', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedInteger('type_id');
        $table->string('product_type');
        $table->timestamps();
    });
}

表格:

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedInteger('type');
        $table->foreign('type')->references('type_id')->on('category');
        $table->string('title');
        $table->string('description');
        $table->integer('price')->nullable();
        $table->mediumtext('image')->nullable();
        $table->timestamps();
    });
}

1 个答案:

答案 0 :(得分:0)

您必须在外键字段上包含onDelete和on Update方法。

    $table->unsignedBigInteger('type');
    $table->foreign('type')->references('type_id')->on('category')->onDelete('cascade')->onUpdate('cascade');

如果不这样做,它还会检查您的迁移顺序。类别迁移应该在帖子迁移之前。