简体   繁体   English

Laravel 5.4:SQLSTATE[HY000]:一般错误:1005 无法创建表“外键约束格式不正确”

[英]Laravel 5.4: SQLSTATE[HY000]: General error: 1005 Can't create table "Foreign key constraint is incorrectly formed"

I'm using Laravel 5.4 and I have added this Migration:我正在使用Laravel 5.4并添加了此迁移:

public function up()
    {
        Schema::create('episodes', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('course_id')->unsigned();
            $table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
            $table->string('type', 10);
            $table->string('title');
            $table->string('slug');
            $table->text('description');
            $table->text('body');
            $table->string('videoUrl');
            $table->string('tags');
            $table->string('time', 15)->default('00:00:00');
            $table->integer('number');
            $table->integer('viewCount')->default(0);
            $table->integer('commentCount')->default(0);
            $table->integer('downloadCount')->default(0);
            $table->timestamps();
        });
    }

Now when I run php artisan migrate , I get this error:现在,当我运行php artisan migrate时,我收到此错误:

SQLSTATE[HY000]: General error: 1005 Can't create table elearning . SQLSTATE[HY000]:一般错误:1005 Can't create table elearning episodes (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table episodes add constraint episodes_course_id_foreign foreign key ( course_id ) references courses ( id ) on delete cascade) episodes (errno:150“外键约束形成错误”)(SQL:alter table episodes添加约束episodes_course_id_foreign外键( course_id )在删除级联时引用coursesid ))

I also tried this but still gets the same error:我也试过这个,但仍然得到同样的错误:

$table->unsignedBigInteger('course_id');

So how can I properly run this Migration?那么我怎样才能正确运行这个迁移呢? I'm really stuck with this, please help me out...我真的被这个困住了,请帮助我......


USERS MIGRATTION:用户迁移:

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('level')->default('user');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

Course Migration :课程迁移

Schema::create('courses', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->string('type', 10);
            $table->string('title');
            $table->string('slug');
            $table->text('description');
            $table->text('body');
            $table->string('price',50);
            $table->string('imageUrl');
            $table->string('tags');
            $table->string('time', 15)->default('00:00:00');
            $table->integer('viewCount')->default(0);
            $table->integer('commentCount')->default(0);
            $table->timestamps();
        });

Rather than use:而不是使用:

$table->increments('id');

you should use:你应该使用:

$table->id();

(it's just easier). (这更容易)。

To create your foreign relationships, rather than have建立你的对外关系,而不是拥有

$table->integer('course_id')->unsigned();
$table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');

you can just use:你可以使用:

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

which will automatically create a column of the right type (unsigned big integer) and then create the relationship by looking for the id column on the courses table.这将自动创建正确类型的列(无符号大整数),然后通过在课程表上查找 id 列来创建关系。

EDIT编辑

As you're using an older version of Laravel, you cannot use the id() function, so just create the course_id column as a big integer:由于您使用的是旧版本的 Laravel,因此您不能使用 id() function,因此只需将 course_id 列创建为大 integer:

$table->bigInteger('course_id')->unsigned();

and then create your relationship as before.然后像以前一样建立你们的关系。

One problem maybe you are not making unsignedBigInteger as course_id一个问题可能是您没有将 unsignedBigInteger 作为 course_id

 Schema::create('episodes', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedBigInteger('course_id')->index('course_id');  
        $table->string('type', 10);
        $table->string('title');
        $table->string('slug');
        $table->text('description');
        $table->text('body');
        $table->string('videoUrl');
        $table->string('tags');
        $table->string('time', 15)->default('00:00:00');
        $table->integer('number');
        $table->integer('viewCount')->default(0);
        $table->integer('commentCount')->default(0);
        $table->integer('downloadCount')->default(0);
        $table->timestamps();

        $table->index( [ 'created_at', 'updated_at' ] );

       //Constraint
        $table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');

   });

OR或者

The second problem may be you are not making your migrations is not ruing sequence, for example, first users table need to be run then courses then episodes第二个问题可能是你没有让你的迁移不是 ruing 顺序,例如,首先需要运行用户表,然后是课程,然后是情节

暂无
暂无

声明:本站的技术帖子网页,遵循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") 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]:一般错误: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 SQLSTATE [HY000]:常规错误:1215无法添加外键约束Laravel 5.4 - SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint Laravel 5.4 一般错误: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") SQLSTATE[HY000]:一般错误:无法创建表 - SQLSTATE[HY000]: General error:Can't create table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM