种子程序在创建记录时跳过id字段



我的一个表的种子程序出现故障

Illuminate\Database\QueryException:SQLSTATE[HY000]:常规错误:1364字段"id"没有默认值(SQL:插入tests(namedescriptionpanel_idunitsminValuelowNotemaxValuehighNotenote_id(值(A/G Ratio,白蛋白与球蛋白比率,2,1.2,2.5,1((

是否注意到id字段被跳过?

以下是迁移的up()方法:

public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->unsignedBigInteger('id');
$table->string('name');
$table->string('description');
$table->unsignedBigInteger('panel_id');
$table->string('units');
$table->string('minValue');
$table->string('lowNote',512);
$table->string('maxValue');
$table->string('highNote',512);
$table->unsignedBigInteger('note_id');
});
}

以下是TestSeeder::run()方法的一部分:

$tests = [
[
'id' => 1,
'name' => 'A/G Ratio',
'description' => 'Albumin to Globulin Ratio',
'panel_id' => 2,
'units' => '',
'minValue' => '1.2',
'lowNote' => '',
'maxValue' => '2.5',
'highNote' => '',
'note_id' => 1
],
];

这是Test型号:

class Test extends Model
{
public $timestamps = false;
protected $fillable = [
'id',
'name',
'description',
'panel_id',
'units',
'minValue',
'lowNote',
'maxValue',
'high_note',
'note_id'
];
}

请注意,id字段列在$fillable数组中,那么为什么要跳过它呢?

您需要更改此行:

$table->unsignedBigInteger('id');

至:

$table->bigIncrements('id');

最新更新