简体   繁体   English

一般错误:1005 无法创建表(错误号:150“外键约束格式不正确”)

[英]General error: 1005 Can't create table (errno: 150 "Foreign key constraint is incorrectly formed")

I have a database Migration in Laravel 8 that goes like this:我在 Laravel 8 中有一个数据库迁移,如下所示:

class CreateArticlesTable extends Migration
{
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->string('title');
            $table->string('slug');
            $table->text('description');
            $table->text('body');
            $table->string('imageUrl');
            $table->string('tags');
            $table->integer('viewCount')->default(0);
            $table->integer('commentCount')->default(0);
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('articles');
    }
}

But whenever I want to run this, I get this error:但是每当我想运行它时,我都会收到此错误:

General error: 1005 Can't create table elearning .一般错误:1005 Can't create table elearning articles (errno: 150 "Foreign key constraint is incorrectly formed") articles (错误号:150“外键约束格式不正确”)

I don't what the heck is going wrong here, so if you know how to solve this issue, please let me know...我不知道这里到底出了什么问题,所以如果你知道如何解决这个问题,请告诉我......

instead of using而不是使用

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

use利用

$table->unsignedBigInteger(); $table->unsignedBigInteger();

laravel uses unsignedBigInteger which is 20 digits and unsignedInteger only takes 11 digits laravel 使用 20 位的 unsignedBigInteger,而 unsignedInteger 只需要 11 位

https://laravel.com/docs/8.x/migrations#foreign-key-constraints https://laravel.com/docs/8.x/migrations#foreign-key-constraints

are the engine of the user's table and the articles table the same and it is both InnoDB?用户表和文章表的引擎是否相同并且都是InnoDB? MyISAM does not support foreign keys. MyISAM 不支持外键。 if it is, is the articles table will create after the user table?如果是,是在用户表之后创建文章表吗? if the answer is yes so: is both fields are exact in the same type?如果答案是肯定的,那么:这两个字段是否完全属于同一类型? I think your user id is autoincremented, if it is, then adds unsigned in foreign key:我认为您的用户 ID 是自动递增的,如果是,则添加未签名的外键:

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

Try this for Laravel 8试试这个 Laravel 8

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

暂无
暂无

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

相关问题 常规错误: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" 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 一般错误: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]: 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 我如何在 Laravel 中修复此错误”1005 无法创建表 `englishcollage`.`role_user`(错误号:150“外键约束的格式不正确” - how can i fix this eror in laravel" 1005 Can't create table `englishcollage`.`role_user` (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" 无法创建表`clothing`.`clothes`(错误号:150“外键约束形成不正确”)”)Laravel 7 - Can't create table `clothing`.`clothes` (errno: 150 "Foreign key constraint is incorrectly formed")") Laravel 7 (错误号:150“外键约束格式不正确”) - (errno: 150 “Foreign key constraint is incorrectly formed”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM