PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 3, 2022

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

 February 03, 2022     laravel, laravel-5, laravel-migrations, mysql, php     No comments   

Issue

I'm using Laravel 5.4 and I have added this Migration:

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:

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)

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

Solution

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.

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 :

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

and then create your relationship as before.



Answered By - Giles Bennett
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing