Не пойму почему не могу сделать миграцию в бд вместе с внешними ключами. Вроде делаю всё правильно и выдаёт ошибку
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `cases` add constraint `cases_categories_id_foreign` foreign key (`categories_id`) references `categories` (`id`))
at /Applications/MAMP/htdocs/laravel/MeowDrop/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
/Applications/MAMP/htdocs/laravel/MeowDrop/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458
2 PDOStatement::execute()
/Applications/MAMP/htdocs/laravel/MeowDrop/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458
Вот миграция cases
Schema::create('cases', function (Blueprint $table) {
$table->bigIncrements('id')->unique();
$table->integer('categories_id')->unsigned();
$table->integer('items_id')->unsigned();
$table->string('name');
$table->string('img');
$table->integer('price');
$table->string('slug');
$table->timestamp('updated_at')->useCurrent();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('deleted_at')->useCurrent();
$table->foreign('categories_id')->references('id')->on('categories')->unsigned();
$table->foreign('items_id')->references('id')->on('items');
});
Миграция categories
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Миграция items
Schema::create('items', function (Blueprint $table) {
$table->increments('id');
$table->integer('assets_id');
$table->string('name');
$table->string('img');
$table->integer('price');
});
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table cases add constraint cases_categories_id_foreign foreign key (categories_id) references categories (id))
Исправил порядок создания таблиц. теперь 2 строки таковы$table->foreign('categories_id')->references('id')->on('categories'); $table->foreign('items_id')->references('id')->on('items');