我正在表中设置外键。当我尝试运行它时,出现错误。
我的迁移:
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/