1

When migrating using the command line, I get this error...

(errno: 150 "Foreign key con straint is incorrectly formed")

And here is my migration code..

public function up()
{
    Schema::enableForeignKeyConstraints();
    Schema::create('student', function (Blueprint $table) {
        $table->integer('student_id')->unsigned()->primary();
        $table->string('student_name', 200);
        $table->string('student_email', 50);
        $table->integer('class_id')->unsigned()->nullable();
        $table->string('password', 100);
        $table->rememberToken();
        $table->timestamps();
    });

    Schema::table('student', function($table) {
       $table->foreign('class_id')->references('class_id')->on('class_tbl');
    });
}

I haven't found solutions to this, and I can't figure out what's wrong...

EDIT:

Here is the migration for class_tbl...

public function up()
{
  Schema::enableForeignKeyConstraints();
  Schema::create('class_tbl', function (Blueprint $table) {
    $table->integer('class_id')->unsigned()->primary();
    $table->string('course', 10);
    $table->string('section', 5);
    $table->string('school_year', 10);
    $table->integer('prof_id')->unsigned();
  });

  Schema::table('class_tbl', function($table) {
    $table->foreign('prof_id')->references('prof_id')->on('professor');
  });
}
  • Could you please add the migration for class_tbl? & which order are the migrations happening in? is class_tbl migrated when student tries to reference it? https://stackoverflow.com/questions/32669880/laravel-migration-foreign-key-constraint-is-incorrectly-formed migration order is related to numeric keys on file-names. – admcfajn Nov 20 '17 at 01:37
  • I believe that the student_tbl is being migrated first, do i have to remake the migration? @admcfajn – DonPatrick15 Nov 20 '17 at 01:42
  • Shouldn't have to remake the migration. Not sure... – admcfajn Nov 20 '17 at 01:53
  • I know i right, I don't know what's causing this. But I'll give the remaking the migration to see if it will work... – DonPatrick15 Nov 20 '17 at 01:54
  • Are you making the base-migrations or building onto a system with pre-existing tables that you can't delete? deleting all tables can be pretty helpful if you're just at the mockup stage. – admcfajn Nov 20 '17 at 01:55
  • Yes, i'm making the base-migration, and have delete all of the tables in the database every time. Also the remaking didn't work. – DonPatrick15 Nov 20 '17 at 01:58

1 Answers1

0

Hmmm... a nullable foreign key? Try this...

Schema::create('student', function (Blueprint $table) {
        $table->integer('student_id')->unsigned()->primary();
        $table->string('student_name', 200);
        $table->string('student_email', 50);
        $table->integer('class_id')->unsigned();
        $table->foreign('class_id')->references('class_id')->on('class_tbl');
        $table->string('password', 100);
        $table->rememberToken();
        $table->timestamps();
    });

   Schema::create('class_tbl', function (Blueprint $table) {
    $table->integer('class_id')->unsigned()->primary();
    $table->string('course', 10);
    $table->string('section', 5);
    $table->string('school_year', 10);
    $table->integer('prof_id')->unsigned();
    $table->foreign('prof_id')->references('prof_id')->on('professor');
  });
Serge
  • 2,107
  • 1
  • 18
  • 24