简体   繁体   English

SQLSTATE[HY000]:一般错误:1005 无法创建表 Laravel 8

[英]SQLSTATE[HY000]: General error: 1005 Can't create table Laravel 8

Schema::create('menus', function (Blueprint $table) {
    $table->id();
    $table->string('name')->unique();
    $table->string('slug')->unique();
    $table->integer('price');
    $table->text('description');
    $table->timestamps();
});

Schema::create('categories', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name')->unique();
    $table->string('slug')->unique();
    $table->timestamps();
});

Schema::create('category_menu', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('menu_id')->unsigned()->nullable();
    $table->foreign('menu_id')->references('id')
        ->on('menus')->onDelete('cascade');
    $table->integer('category_id')->unsigned()->nullable();
    $table->foreign('category_id')->references('id')
        ->on('categories')->onDelete('cascade');
    $table->timestamps();
});

When I run php artisan:migrate , I get the following error.当我运行php artisan:migrate时,出现以下错误。

SQLSTATE[HY000]: General error: 1005 Can't create table `mieaceh`.`category_menu` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `category_menu` add constraint `category_menu_menu_id_foreign` foreign key (`menu_id`) references `menus` (`id`) on delete cascade) SQLSTATE[HY000]: General error: 1005 Can't create table `mieaceh`.`category_menu` (errno: 150 "Foreign key constraint is wrongly forms") (SQL: alter table `category_menu` add constraint `category_menu_menu_id_foreign`外键( `menu_id`) 在删除级联时引用`menus` (`id`)

This is due to mismatch of datatype of foreign key column and the referenced column most likely这是由于外键列和被引用列的数据类型很可能不匹配

When you create a primary key with $table->id() the datatype of the auto incrementing id column is unsignedBigInteger so you must have foreign key also with the same datatype当您使用$table->id()创建主键时,自动递增 id 列的数据类型是unsignedBigInteger因此您必须具有相同数据类型的外键

Schema::create('category_menu', function (Blueprint $table) {
        $table->id();

        $table->unsignedBigInteger('menu_id');
        $table->foreign('menu_id')->references('id')
            ->on('menus')->onDelete('cascade');

        $table->unsignedBigInteger('category_id');
        $table->foreign('category_id')->references('id')
            ->on('categories')->onDelete('cascade');

        $table->timestamps();
});

You shouldn't make the columns in a pivot table nullable for the foreign keys to maintain data integrity.您不应该将 pivot 表中的列设置为可以为外键为空以保持数据完整性。

Also be consistent with the datatype for primary key columns as well as definitions - when using $table->id() keep that consistent across all migrations for all tables.还要与主键列的数据类型和定义保持一致——使用$table->id()时,在所有表的所有迁移中保持一致。 That way you will have less chances of mismatch when defining foreign keys as $table->unsignedBigInteger()这样,在将外键定义为$table->unsignedBigInteger()时,您将有更少的不匹配机会

laravel migrations files contains a datetime slug that determine which file will be migrated first laravel 迁移文件包含一个日期时间段,用于确定首先迁移哪个文件

notice the order注意顺序

2021_10_11_101533_create_posts_table.php
2014_10_12_000000_create_users_table.php

when you run当你跑步时

php artisan migrate 

posts table will be migrated first and it contains a foriegn key帖子表将首先迁移,它包含一个外键

user_id用户身份

which references a primary key on users table它引用用户表上的主键

id ID

but the users table doesn't exists yet, to fix this issues change the files names and make sure users table migrate first like this但是用户表还不存在,要解决此问题,请更改文件名并确保用户表首先像这样迁移

notice the order注意顺序

2014_10_10_000000_create_users_table.php
2021_10_11_101533_create_posts_table.php

short pattern for declaring a foreign key用于声明外键的简短模式

$table->foreignId('user_id')->constrained()->cascadeOnDelete();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLSTATE [HY000]:一般错误:1005 无法创建表 - Laravel 4 - SQLSTATE[HY000]: General error: 1005 Can't create table - Laravel 4 Laravel 5.4:SQLSTATE[HY000]:一般错误:1005 无法创建表“外键约束格式不正确” - Laravel 5.4: SQLSTATE[HY000]: General error: 1005 Can't create table "Foreign key constraint is incorrectly formed" SQLSTATE[HY000]:一般错误:无法创建表 - SQLSTATE[HY000]: General error:Can't create table SQLSTATE[HY000]: 一般错误: 1005 Can't create table `school`.`posts` (errno: 150 "Foreign key constraint is wrongly forms") - SQLSTATE[HY000]: General error: 1005 Can't create table `school`.`posts` (errno: 150 "Foreign key constraint is incorrectly formed") SQLSTATE[HY000]: General error: 1005 Can't create table `business`.`users` (errno: 150 "Foreign key constraint is incorrectly formed") Laravel 7 - SQLSTATE[HY000]: General error: 1005 Can't create table `business`.`users` (errno: 150 "Foreign key constraint is incorrectly formed") Laravel 7 工匠迁移结果:SQLSTATE [HY000]:一般错误:LUMEN / LARAVEL上为1005 - artisan migrate results: SQLSTATE[HY000]: General error: 1005 on LUMEN / LARAVEL 找不到表(SQLSTATE [HY000]:常规错误:1无此类表:用户) - Can't find table (SQLSTATE[HY000]: General error: 1 no such table: users) 错误1005(HY000):无法创建表“ db.POSTS”(错误号:150) - ERROR 1005 (HY000): Can't create table 'db.POSTS' (errno: 150) SQLSTATE [HY000]:一般错误:Laravel发生2053错误 - SQLSTATE[HY000]: General error: 2053 error occurs at Laravel 更新表格错误? (SQLSTATE [HY000]:一般错误) - Updating table error? (SQLSTATE[HY000]: General error)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM