Save only one of multiple hasOne-

Hey everyone,

I have the following problem:

supplies hasMany batches
batches hasOne flatsteel
batches hasOne roundsteel
batches hasOne steelpipe

I add a supply with multiple batches, works fine so far.

Even for every batch a flatsteel, a roundsteel and a steelpipe is created.
But I only want either a flatsteel, a roundsteel or a steelpipe (just one of them, not all).

CREATE TABLE batches (
 id INT AUTO_INCREMENT PRIMARY KEY,
 length SMALLINT NOT NULL,
 location_id INT NOT NULL,
 material_id INT NOT NULL,
 supply_id INT NOT NULL,
 flatsteel_id INT UNIQUE,
 roundsteel_id INT UNIQUE,
 steelpipe_id INT UNIQUE,
 FOREIGN KEY location_key (location_id) REFERENCES locations(id),
 FOREIGN KEY material_key (material_id) REFERENCES materials(id),
 FOREIGN KEY supply_key (supply_id) REFERENCES supplies(id),
 FOREIGN KEY flatsteel_key (flatsteel_id) REFERENCES flatsteels(id),
 FOREIGN KEY roundsteel_key (roundsteel_id) REFERENCES roundsteels(id),
 FOREIGN KEY steelpipe_key (steelpipe_id) REFERENCES steelpipes(id)
);

CREATE TABLE flatsteels (
 id INT AUTO_INCREMENT PRIMARY KEY,
 height SMALLINT NOT NULL,
 width SMALLINT NOT NULL
);

SuppliesController.php:
 $supply = $this->Supplies->patchEntity($supply, $data = $this->request->getData();, [
  'associated' => [
 'Batches',
 'Batches.Locations',
 'Batches.Materials',
 'Batches.Flatsteels',
 'Batches.Roundsteels',
  'Batches.Steelpipes'
 ]
 ]);
 
 add.ctp:
  echo $this->Form->control('batches.'.$i.'.material_id', ['options' => $materials]);
 echo $this->Form->control('batches.'.$i.'.flatsteel.width', ['error' => false]);
 echo $this->Form->control('batches.'.$i.'.flatsteel.height', ['error' => false]);
        echo $this->Form->control('batches.'.$i.'.roundsteel.diameter', ['error' => false]);
        echo $this->Form->control('batches.'.$i.'.steelpipe.outerdiameter', ['error' => false]);
        echo $this->Form->control('batches.'.$i.'.steelpipe.innerdiameter', ['error' => false]);
        echo $this->Form->control('batches.'.$i.'.length');
        echo $this->Form->control('batches.'.$i.'.location_id', ['options' => $locations]);

Thanks a lot!

So, even though your associations are hasOne, you don’t always have one?