我有自相关表:
Schema::create('regions', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('geoname')->unique();
$table->enum('type', ['continent', 'region', 'country', 'state', 'city'])->index();
$table->string('code', 2)->nullable()->index();
$table->string('name');
$table->string('language', 2)->nullable()->index();
$table->unsignedBigInteger('population')->nullable();
$table->timestamps();
$table->unique(['code', 'type']);
});
Schema::create('regions_has_regions', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('region_id');
$table->unsignedBigInteger('has_region_id');
$table->timestamps();
$table->foreign('region_id')->references('geoname')->on('regions');
$table->foreign('has_region_id')->references('geoname')->on('regions');
});
我想使用geoname
作为主键(因为它是全局唯一的整数标识符(
型号有:
protected $primaryKey = 'geoname';
public function children(): BelongsToMany
{
return $this->belongsToMany(
Region::class,
'regions_has_regions',
'region_id',
'has_region_id',
'geoname',
'geoname',
'children');
}
public function parents(): BelongsToMany
{
return $this->belongsToMany(
Region::class,
'regions_has_regions',
'has_region_id',
'region_id',
'geoname',
'geoname',
'parents');
}
主键和关系键设置为geoname
。
但在这里,我进口各大洲的国家:
foreach ($continents as $continent) {
$pop = 0;
$continent->children()->saveMany(
collect($earth->find(['continent' => $continent->code])->useShortNames()->toArray())
->map(function ($c) use ($continent, $now, &$pop) {
$pop += $c['population'];
return Region::create([
'code' => $c['isoCode'],
'name' => $c['name'],
'type' => 'country',
'language' => $c['language'],
'population' => $c['population'],
'geoname' => $c['geonamesCode'],
]);
})
);
$continent->update(['population' => $pop]);
}
然后得到一个错误:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`homestead`.`regions_has_regions`, CONSTRAINT `regions_has_regions_has_region_id_foreign` FOREIGN KEY (`has_region_id`) REFERENCES `regions` (`geoname`)) (SQL: insert into `regions_has_regions` (`has_region_id`, `region_id`) values (7, 6255146))
如您所见,它尝试将id
值插入到has_region_id
,而不是geoname
。
如何解决此问题?
好的,计算出来:模型应该有这个:
public $incrementing = false;