简体   繁体   English

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

I need to create a one to many relationship between users (created from laravel 7 auth scaffolding) and categories.我需要在用户(从 laravel 7 auth 脚手架创建)和类别之间创建一对多关系。 But when I run migration I get this error: Migrating: 2014_10_12_000000_create_users_table但是当我运行迁移时,我得到这个错误:迁移:2014_10_12_000000_create_users_table

Illuminate\Database\QueryException照亮\数据库\查询异常

SQLSTATE[HY000]: General error: 1005 Can't create table business . SQLSTATE[HY000]: General error: 1005 Can't create table business users (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table users add constraint users_category_id_foreign foreign key ( category_id ) references catgories ( id ) on delete cascade) users (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table users add constraint users_category_id_foreign外键 ( category_id ) references catgories ( id ) on delete cascade)

here is the users schema:这是用户模式:

Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('category_id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();

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

    });

And this the on of categories:这是类别:

Schema::create('categories', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('category');
        $table->timestamps();
    });

I have done some searches and I found out that there are two reasons for such an error, the first is that the categories table migration should be done before the users table one, and the second is that the category id datatype on the users schema and the one of the id on the category must be the same, I verified both of tese conditions and I am still getting the same error.我做了一些搜索,我发现这种错误有两个原因,第一个是类别表迁移应该在用户表之前完成,第二个是用户模式上的类别 id 数据类型和类别中的一个 id 必须相同,我验证了这两个条件,但我仍然遇到相同的错误。 I would be so grateful if someone can tell me what's wrong!如果有人能告诉我出了什么问题,我将不胜感激!

Change on('categories') instead of on('catgories') You wrote catgories .更改on('categories')而不是on('catgories')你写了catgories But it must be categories但它必须是categories

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

That happens when the types of the Foreign key are not matching the type of the primary key.当外键的类型与主键的类型不匹配时,就会发生这种情况。

Make sure that category_id is the exact same type and length then the primary key of the category table categories.确保category_id的类型和长度与类别表类别的主键完全相同。 So if you declared as an unsigned big integer, make sure categories.id is an unsigned big integer.因此,如果您声明为无符号大 integer,请确保categories.id为无符号大 integer。

暂无
暂无

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

相关问题 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") Laravel 5.4:SQLSTATE[HY000]:一般错误:1005 无法创建表“外键约束格式不正确” - Laravel 5.4: SQLSTATE[HY000]: General error: 1005 Can't create table "Foreign key constraint is incorrectly formed" 一般错误:1005 无法创建表(错误号:150“外键约束格式不正确”) - General error: 1005 Can't create table (errno: 150 "Foreign key constraint is incorrectly formed") 常规错误:1005无法创建表errno:150“外键约束格式错误”) - General error: 1005 Can't create table errno: 150 “Foreign key constraint is incorrectly formed”) 一般错误:1005 无法创建表...(errno:150“外键约束格式不正确” - General error : 1005 Can't create table ... (errno:150 "Foreign key constraint is incorrectly formed" Laravel 一般错误:1005 无法创建表`categories_products`(错误号:150“外键约束的格式不正确”) - Laravel General error: 1005 Can't create table `categories_products` (errno: 150 "Foreign key constraint is incorrectly formed") : 1005 无法创建表 `shop`.`role_user` (errno: 150 “外键约束格式不正确”)") - : 1005 Can't create table `shop`.`role_user` (errno: 150 “Foreign key constraint is incorrectly formed”)") SQLSTATE[HY000]:一般错误:1005 无法创建表 Laravel 8 - SQLSTATE[HY000]: General error: 1005 Can't create table Laravel 8 SQLSTATE [HY000]:一般错误:1005 无法创建表 - Laravel 4 - SQLSTATE[HY000]: General error: 1005 Can't create table - Laravel 4 错误1005(HY000):无法创建表“ db.POSTS”(错误号:150) - ERROR 1005 (HY000): Can't create table 'db.POSTS' (errno: 150)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM