【发布时间】:2019-12-16 15:57:55
【问题描述】:
你能帮帮我吗?
我使用 MySQL。
我做
php artisan migrate
但是错误
Illuminate\Database\QueryException : SQLSTATE[HY000]: 一般错误: 1005 无法创建表
learn.products(errno: 150 "外键 约束格式不正确”)(SQL:alter tableproductsadd 约束products_category_id_foreign外键(category_id) 参考categories(id))
Categories迁移:
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 50)->unique();
$table->timestamps();
$table->softDeletes();
});
Products迁移:
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 150)->unique();
$table->unsignedBigInteger('category_id');
$table->string('image', 255);
$table->boolean('sale')->default(false);
$table->text('description');
$table->timestamps();
$table->softDeletes();
$table->foreign('category_id')->references('id')->on('categories');
});
【问题讨论】:
-
这是因为您的
products.category_id列和您的categories.id列之间的类型不匹配。您要么需要将id列设为bigIncrements,要么将category_id设为->integer('category_id')->unsigned()
标签: php mysql laravel laravel-5 migration