laravel 迁移外键可为空
laravel migration foreign key nullable
我正在使用 Laravel 版本 7.25.0 构建一个项目。我对我的迁移文件进行了编码,它们包含一些外键。我在每个外键的末尾添加了 nullable() 但它们不起作用。我查看了类似的问题,但其中 none 解决了我的问题。这是我的迁移文件的 up 函数
{
Schema::create('tests', function (Blueprint $table) {
$table->id();
$table->string('title')->nullable();
$table->tinyInteger('level')->nullable();
$table->tinyInteger('try_count')->nullable();
$table->char('room_name')->nullable();
$table->boolean('timeless')->nullable();
$table->integer('time')->nullable();
$table->foreignId('school_id')->constrained('schools')->nullable();
$table->foreignId('year_id')->constrained('years')->nullable();
$table->foreignId('course_id')->constrained('courses')->nullable();
$table->string('type')->nullable();
$table->date('start_date')->nullable();
$table->date('end_date')->nullable();
$table->time('start_time')->nullable();
$table->time('end_time')->nullable();
$table->text('description')->nullable();
$table->boolean('timeout')->nullable();
$table->boolean('question_sorting')->nullable();
$table->timestamps();
});
}
当我将它与其他文件一起迁移并且没有出现任何错误时,这是我的那个 table
的 phpmyadmin 页面
phpadmin page table ss
必须在约束之前调用任何额外的列修饰符,
所以你必须像这样把 nullable() 放在 constrained() 之前:
$table->foreignId('course_id')->nullable()->constrained('courses');
我正在使用 Laravel 版本 7.25.0 构建一个项目。我对我的迁移文件进行了编码,它们包含一些外键。我在每个外键的末尾添加了 nullable() 但它们不起作用。我查看了类似的问题,但其中 none 解决了我的问题。这是我的迁移文件的 up 函数
{
Schema::create('tests', function (Blueprint $table) {
$table->id();
$table->string('title')->nullable();
$table->tinyInteger('level')->nullable();
$table->tinyInteger('try_count')->nullable();
$table->char('room_name')->nullable();
$table->boolean('timeless')->nullable();
$table->integer('time')->nullable();
$table->foreignId('school_id')->constrained('schools')->nullable();
$table->foreignId('year_id')->constrained('years')->nullable();
$table->foreignId('course_id')->constrained('courses')->nullable();
$table->string('type')->nullable();
$table->date('start_date')->nullable();
$table->date('end_date')->nullable();
$table->time('start_time')->nullable();
$table->time('end_time')->nullable();
$table->text('description')->nullable();
$table->boolean('timeout')->nullable();
$table->boolean('question_sorting')->nullable();
$table->timestamps();
});
}
当我将它与其他文件一起迁移并且没有出现任何错误时,这是我的那个 table
的 phpmyadmin 页面phpadmin page table ss
必须在约束之前调用任何额外的列修饰符,
所以你必须像这样把 nullable() 放在 constrained() 之前:
$table->foreignId('course_id')->nullable()->constrained('courses');