Can you help me?
I use the MySQL.
I make
php artisan migrate
but error
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table
learn
.products
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tableproducts
add constraintproducts_category_id_foreign
foreign key (category_id
) referencescategories
(id
))
Categories
migration:
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 50)->unique();
$table->timestamps();
$table->softDeletes();
});
Products
migration:
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
column and yourcategories.id
column. You either need to make yourid
columnsbigIncrements
or make yourcategory_id
an->integer('category_id')->unsigned()
– lufc