Search code examples
phpmysqllaravelmigrate

laravel migration to add foreign key


I'm trying to add foreign key in my table exam:

public function up()
    {
        Schema::create('exams', function (Blueprint $table) {
            $table->id();
            $table->BigInteger('student_id')->unsigned()->nullable();
            $table->BigInteger('subject_id')->unsigned()->nullable();
            $table->integer('mark');
            $table->timestamps();
            $table->foreign('student_id')
            ->references('id')
            ->on('students');
            $table->foreign('subject_id')
            ->references('id')
            ->on('subjects');
        });
    }

Student's table:

public function up()
{
    Schema::create('students', function (Blueprint $table) {
        $table->id();
        $table->string('fname');
        $table->string('mname');
        $table->string('lname');
        $table->timestamps();
    });
}

Subject's table:

Schema::create('subjects', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->timestamps();
        });

I have searched a lot and try it with unsignedBigInteger or $table->bigInteger('student_id')->unsigned()->nullable(); and try this:

Laravel migration: "Foreign key constraint is incorrectly formed" (errno 150)

But this error is still happening when I use php artisan migrate What can i do?

Illuminate\Database\QueryException

SQLSTATE[HY000]: General error: 1005 Can't create table salamstu.exams (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table exams add constraint exams_student_id_foreign foreign key (student_id) references students (id) on delete cascade)

at C:\Users\User\Documents\laravel\SalamStu\vendor\laravel\framework\src\Illuminate\Database\Connection.php:716 712▕ // If an exception occurs when attempting to run a query, we'll format the error 713▕ // message to include the bindings with SQL, which will make this exception a 714▕ // lot more helpful to the developer instead of just the database's errors. 715▕ catch (Exception $e) { ➜ 716▕ throw new QueryException( 717▕ $query, $this->prepareBindings($bindings), $e 718▕ ); 719▕ } 720▕ }

1
C:\Users\User\Documents\laravel\SalamStu\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501 PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table salamstu.exams (errno: 150 "Foreign key constraint is incorrectly formed")")

2
C:\Users\User\Documents\laravel\SalamStu\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501 PDOStatement::execute()


Solution

  • $table->bigIncrements('id'); = Auto-incrementing UNSIGNED BIGINT (primary key) equivalent column.

    $table->increments('id'); = Auto-incrementing UNSIGNED INTEGER (primary key) equivalent column.

    INT[(M)] [UNSIGNED] [ZEROFILL]

    A normal-size integer. The signed range is -2147483648 to 2147483647. The unsigned range is 0 to 4294967295.

    BIGINT[(M)] [UNSIGNED] [ZEROFILL]

    A large integer. The signed range is -9223372036854775808 to 9223372036854775807. The unsigned range is 0 to 18446744073709551615.

    For $table->increments('id');, use this:

    $table->unsignedInteger('student_id')->nullable();
    $table->foreign('student_id')->references('id')->on('students');
    

    For $table->bigIncrements('id');, use this:

    $table->unsignedBigInteger('student_id')->nullable();
    $table->foreign('student_id')->references('id')->on('students');