Я пытаюсь настроить базу данных с несколькими таблицами, здесь актуально:
Таблица: Коллекции:
Код: Выделить всё
Schema::create('collections', function (Blueprint $table) {
$table->id();
$table->foreignId('owner_id')->constrained(table:'users', indexName: 'collections_users_id');
$table->foreignId('library_id')->nullable()->constrained(table:'libraries', indexName: 'collections_libraries_id');
[...]
$table->timestamps();
});
Код: Выделить всё
Schema::create('libraries', function (Blueprint $table) {
$table->id();
$table->foreignId('creator_id')->constrained(table: 'users', indexName: 'collections_users_id');
[...] $table->timestamps();
});
Код: Выделить всё
SQLSTATE[HY000]: General error: 1005 Can't create table `TABLENAME`.`collections` (errno: 150 "Foreign key constraint is incorrectly formed") (Connection: mysql, SQL: alter table `collections` add constraint `collections_library_id_foreign` foreign key (`library_id`) references `libraries` (`id`))
Подробнее здесь: https://stackoverflow.com/questions/785 ... ent-tables
Мобильная версия