mysql - 如何修复迁移 Laravel 中的 'error : SQLSTATE[HY000]'

标签 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)

最佳答案

确保您的 products 迁移和 users 迁移表中的 user_id 类型匹配。

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

关于mysql - 如何修复迁移 Laravel 中的 'error : SQLSTATE[HY000]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57895389/

相关文章:

javascript - 应用 laravel spatie/CSP 后猫头鹰轮播列表不会显示

mysql - View 和 EER 图有什么区别?

php - Laravel 5.1 在 url 中添加查询字符串

laravel - 在Laravel中向Eloquent模型添加自定义方法

php - 如何通过 Laravel Blade 在数组内部打印数组?

laravel - 如何重构此搜索过滤器查询?

php - 从数据库打印数据时出错;打印多次

mysql - 如何使用 Sequelize 将每行 2 列相乘并对所有结果求和?

mysql - 关于查询和子查询

php - MySQL中如何根据关键字过滤数据?