【问题标题】:Laravel 5.4: SQLSTATE[HY000]: General error: 1005 Can't create table "Foreign key constraint is incorrectly formed"Laravel 5.4:SQLSTATE[HY000]:一般错误:1005 无法创建表“外键约束格式错误”
【发布时间】:2021-12-29 11:42:56
【问题描述】:

我正在使用 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();
        });
    }

现在当我运行 php artisan migrate 时,我得到了这个错误:

SQLSTATE[HY000]: 一般错误: 1005 Can't create table elearning.episodes (errno: 150 "外键约束格式不正确") (SQL: alter table episodes add constraint episodes_course_id_foreign外键(course_id)在删除级联时引用coursesid

我也试过了,但还是报同样的错误:

$table->unsignedBigInteger('course_id');

那么我怎样才能正确地运行这个迁移呢?我真的被这个困住了,请帮帮我......


用户迁移:

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();
        });

课程迁移

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();
        });

【问题讨论】:

  • 关于这个错误已经有很多问题了。你有没有尝试从这些中找到解决方案??
  • @zahidhasanemon 我已经尝试过但无法解决问题:((
  • 然后在问题中分享你的课程表迁移和迁移顺序。
  • @zahidhasanemon 我也试过bigIncrements而不是increments,但仍然出现错误
  • @zahidhasanemon 我刚刚添加了与此相关的users 迁移,请检查一下

标签: php mysql laravel laravel-5 laravel-migrations


【解决方案1】:

而不是使用:

$table->increments('id');

你应该使用:

$table->id();

(更简单)。

建立你的对外关系,而不是拥有

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

你可以使用:

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

这将自动创建一个正确类型的列(无符号大整数),然后通过在课程表上查找 id 列来创建关系。

编辑

由于你使用的是旧版本的 Laravel,你不能使用 id() 函数,所以只需将 course_id 列创建为一个大整数:

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

然后像以前一样建立你们的关系。

【讨论】:

  • 如果我使用$table->id();,我会得到Call to undefined method Illuminate\Database\Schema\Blueprint::id(),因为我的版本是5.4
  • OK - 你需要检查使用 $table->increments('id');创建 - 如果它为您的课程表上的 ID 列创建一个大整数,那么您的 episodes 表上的 course_id 列也需要是一个大整数,在这种情况下,您的第一行应该是 $table->bigInteger('course_id' )->无符号();
【解决方案2】:

一个问题可能你没有将 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');

   });

第二个问题可能是你没有让你的迁移不是 ruing 顺序,例如,首先需要运行 users 表,然后是课程,然后是情节

【讨论】:

  • 这里的$table->index( [ 'created_at', 'updated_at' ] ); 是什么意思?
  • 将您的 created_at 和 updated_at 作为索引,以便您的数据可以更快地检索
猜你喜欢
  • 2020-12-19
  • 2021-03-20
  • 2014-01-26
  • 2020-12-02
  • 2021-09-23
  • 2020-11-27
  • 2018-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多