【发布时间】:2020-06-12 20:49:40
【问题描述】:
我有两个模型:
类别
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title',10);
$table->timestamps();
});
}
产品
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('category_id')->index('category_id');
$table->string('title', 20);
$table->text('description');
$table->timestamps();
$table->foreign('category_id')->references('id')->on('categories');
});
}
当我运行 php artisan make:migrate 时,我收到此错误:
Illuminate\Database\QueryException : SQLSTATE[HY000]: 一般错误: 1215 无法添加外键约束(SQL:alter table
products添加约束products_category_id_foreign外键 (category_id) 引用categories(id))
我做错了什么?
【问题讨论】:
-
您试图在
signed大整数和unsigned大整数之间放置一个外键。使它们都未签名.... -
您的代码是正确的。你没有任何错误
-
不,这是不可能的,因为如果我用 $table->foreign('category_id')->references('id')->on('categories');表用户 id 没有问题是 bigIncrement
标签: php mysql laravel migration