Это моя students
миграция, и я хочу иметь внешний ключ с именем subject_id
:
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('first_name');
$table->string('last_name');
$table->string('student_code');
$table->string('national_code');
$table->integer('subject_id')->unsigned();
$table->foreign('subject_id')
->references('id')->on('subjects');
$table->timestamps();
});
}
и это моя subjects
миграция:
public function up()
{
Schema::create('subjects', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->timestamps();
});
}
Моя проблема очень проста. Я искал в документе и ничего не нашел, кроме кодов миграции. Я сбит с толку.
во всяком случае, сначала я запустил subjects
сценарий миграции, затем students
, но я получаю странную ошибку:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table students add constraint students_subject_id_foreign foreign key (subject_id) references subjects (id))