laravel 迁移添加外键

laravel migration to add foreign key

我正在尝试在我的 table 考试中添加外键:

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

学生table:

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

主题的table:

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

我搜索了很多,用 unsignedBigInteger$table->bigInteger('student_id')->unsigned()->nullable(); 试试这个:

但是当我使用php artisan migrate时仍然出现这个错误 我能做什么?

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

$table->bigIncrements('id'); = Auto-incrementing UNSIGNED BIGINT (primary key) 等效列。

$table->increments('id'); = Auto-incrementing UNSIGNED INTEGER (primary key) 等效列。

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

一个normal-size整数。带符号的范围是 -21474836482147483647unsigned 范围是 04294967295

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

一个大整数。 signed 的范围是 -92233720368547758089223372036854775807。无符号范围是 018446744073709551615.

对于$table->increments('id');,使用这个:

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

对于$table->bigIncrements('id');,使用这个:

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