I have a products table and a product_exchanges table that has references products.id as a foreign. However, when I run the Laravel migration I got this error:
SQLSTATE[HY000]: General error: 1005 Can't create table `test`.`product_exchanges` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `product_exchanges` add constraint `product_exchanges_product_id_foreign` foreign key (`product_id`) references `products` (`id`) on delete cascade on update cascade)
product table
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->text('name');
$table->boolean('active')->default(1);
$table->timestamps();
});
products_exchanes table
Schema::create('product_exchanges', function (Blueprint $table) {
$table->id();
$table->dateTime('date');
$table->double('amount')->unsigned();
$table->integer('count_from')->unsigned();
$table->integer('count_to')->unsigned();
$table->bigInteger('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products')->onUpdate('cascade')->onDelete('cascade');
$table->timestamps();
});
and I already put the products table before the product_exchanges table
I also made new two tables that look like this relation and get the same error