При выполнении миграции последняя таблица заканчивается ошибкой в заголовке.
Моя база данных является локальной MySQL.
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table
shopping_list_develop.#sql-1698_2b(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tableproductsadd constraintproducts_shopping_list_id_foreignforeign key (shopping_list_id) referencesshopping_lists(id) on delete set null on update cascade)
Вот что я проверяю:
shopping_lists) создается перед дочерней таблицей (products).shopping_list_id имеет тот же тип столбца, на который он ссылается.shopping_lists и products, имеют один и тот же движок БД (InnoDB).Я читал другие ответы, но не могу найти решение.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Carbon\Carbon;
class CreateShoppingListsTable extends Migration {
public function up()
{
Schema::create('shopping_lists', function(Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable()->default(Carbon::now()->toDateString());
$table->unsignedBigInteger('user_id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});
}
public function down()
{
Schema::table('shopping_lists', function(Blueprint $table) {
$table->dropForeign(['user_id']);
});
Schema::dropIfExist('shopping_lists');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateProductsTable extends Migration {
public function up()
{
Schema::create('products', function(Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('brand')->nullable()->default(null);
$table->float('price', 6,2)->nullable()->default(null);
$table->string('note')->nullable()->default(null);
$table->string('salable_type');
$table->unsignedBigInteger('salable_id');
$table->unsignedBigInteger('shopping_list_id');
$table->timestamps();
$table->foreign('shopping_list_id')->references('id')->on('shopping_lists')
->onDelete('set null')
->onUpdate('cascade');
});
}
public function down()
{
Schema::table('products', function(Blueprint $table) {
$table->dropForeign(['shopping_list_id']);
});
Schema::dropIfExist('products');
}
}
Спасибо за помощь и спросите меня любую дополнительную информацию, если вам нужно.






В вашей миграции 2019_05_13_192700_create_products_table.php onDelete вы устанавливаете значение null.
$table->foreign('shopping_list_id')->references('id')->on('shopping_lists')
->onDelete('set null')
->onUpdate('cascade');
Но объявленный вами столбец не имеет значения NULL
$table->unsignedBigInteger('shopping_list_id');
Попробуйте сделать этот столбец обнуляемым, выполнив вместо этого следующее:
$table->unsignedBigInteger('shopping_list_id')->nullable();