如何解决迁移laravel中的“错误:SQLSTATE [HY000]”

时间:2019-09-11 19:04:35

标签: mysql laravel

我在表中设置外键。当我尝试运行它时,出现错误。

我的迁移:

Schema::create('products', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name');
    $table->integer('user_id')->unsigned();
    $table->integer('camera');
    $table->integer('weight');
    $table->integer('price');
    $table->string('barcode');
    $table->date('Production_at');
    $table->timestamps();

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

错误:

Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `phoneshop`.`#sql-1e8_70` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `products` add constraint `products_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade on update cascade)

1 个答案:

答案 0 :(得分:0)

确保在user_id迁移和products迁移表中的users上具有匹配类型。

默认情况下,laravel users表使用bigIncrements,因此您必须使用bigInteger作为列类型。