That means that you don't have the same datatype on your foreign key as on your id. They must be the same type.
General error: 1005 Can't create table (errno: 150 "Foreign key constraint is incorrectly formed")
I'm using Laravel 5.4 and I have made this Migration:
public function up()
{
Schema::create('episodes', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('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();
});
}
But when I run this Migration I get this error:
SQLSTATE[HY000]: General error: 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)
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table `elearning`.`episodes` (errno: 150 "Foreign key constraint is incorrectly formed")```
I also tried `$table->integer('course_id')->unsigned();` and `$table->unsignedBigInteger('course_id')->unsigned();` instead of `$table->bigInteger('course_id')->unsigned();` but still gets the same error.
And here is Course Migration related to this:
public function up() { Schema::create('courses', function (Blueprint $table) { $table->increments('id'); $table->bigInteger('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(); }); }
So what's going wrong here, How can I solve this issue?
I would really appreciate any idea or suggestion from you guys...
Thanks in advance.
Please or to participate in this conversation.