【发布时间】: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)在删除级联时引用courses(id)
我也试过了,但还是报同样的错误:
$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