Php laravel迁移SQLSTATE[HY000]:一般错误:1215无法添加外键约束

Php laravel迁移SQLSTATE[HY000]:一般错误:1215无法添加外键约束,php,laravel,schema,Php,Laravel,Schema,当我试图在laravel中运行迁移命令时,我得到了这个错误 Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `posts` add constraint `posts_user_id_foreign` foreign key (`user_id`) references `users` (`

当我试图在laravel中运行迁移命令时,我得到了这个错误

 Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `posts` add constraint `posts_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)
在您将此问题标记为重复或投票否决之前,请阅读 这是一个完整的问题

我在这个网站上找到的解决方案是:

标记为未签名我有 在2步中生成整数,我有 模式顺序问题我提供了截图,以了解情况并非如此 我不确定为什么会出现这种错误,以下是我的代码:

Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('slug')->unique();
            $table->longText('body');
            $table->string('photo');
            $table->text('meta_description')->nullable();
            $table->text('meta_tags')->nullable();
            $table->integer('user_id')->unsigned();
            $table->string('publish')->default('0');
            $table->string('comment')->default('0');
            $table->timestamps();
});
Schema::table('posts', function (Blueprint $table) {
  $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
有什么想法吗

使现代化 用户模式

Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('photo')->nullable();
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
});
users表中的主键是BigIncrements,它创建了一个无符号的大整数列,但posts表中的外键是integer,因此它们的类型不同

将外键更改为bigInteger将修复它

因此:

$table->bigInteger('user_id')->unsigned();
而不是:

$table->integer('user_id')->unsigned();
users表中的主键是BigIncrements,它创建了一个无符号的大整数列,但posts表中的外键是integer,因此它们的类型不同

将外键更改为bigInteger将修复它

因此:

$table->bigInteger('user_id')->unsigned();
而不是:

$table->integer('user_id')->unsigned();

只需替换此代码::

Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('slug')->unique();
            $table->longText('body');
            $table->string('photo');
            $table->text('meta_description')->nullable();
            $table->text('meta_tags')->nullable();
            $table->bigInteger('user_id')->unsigned();
            $table->string('publish')->default('0');
            $table->string('comment')->default('0');
            $table->timestamps();

            $table->foreign('user_id')
               ->references('id')
               ->on('users')
               ->onDelete('cascade');

});

只需替换此代码::

Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('slug')->unique();
            $table->longText('body');
            $table->string('photo');
            $table->text('meta_description')->nullable();
            $table->text('meta_tags')->nullable();
            $table->bigInteger('user_id')->unsigned();
            $table->string('publish')->default('0');
            $table->string('comment')->default('0');
            $table->timestamps();

            $table->foreign('user_id')
               ->references('id')
               ->on('users')
               ->onDelete('cascade');

});

请为用户发布迁移文件table@AhmedNourJamalEl-Din Updated请尝试将外键更改为biginger,如下所示:$table->biginger'user\u id'->unsigned@艾哈迈德·努尔贾马莱尔·丁很奇怪!它现在起作用了:谢谢你,它起作用了。我回答了,所以人们可以得到一些帮助。请为用户发布迁移文件table@AhmedNourJamalEl-Din Updated请尝试将外键更改为biginger,如下所示:$table->biginger'user\u id'->unsigned@艾哈迈德·努尔贾马莱尔·丁很奇怪!它现在起作用了:谢谢你,它起作用了。我回答了,这样人们可以得到一些帮助