lodata icon indicating copy to clipboard operation
lodata copied to clipboard

Upserting Eloquent Model fails, alternative Key ignored

Open t0815 opened this issue 7 months ago • 0 comments

Hi there,

i want to upsert an Eloquent Model, but when sending a patchrequest, im getting the following error:

{
  "error": {
    "code": "cannot_upsert_computed_key",
    "message": "Cannot upsert on an entity set with a computed key",
    "target": null,
    "details": [],
    "innererror": {}
  }
}

The Model has default Primary KEY id, auto-increment. Is it not possible to upsert Models with Autoincrement Keys?

When setting an alternative Key for the Entity, using:

$recordType = Lodata::getEntityType('application_record');
$recordType->getProperty('record_number')->setAlternativeKey();

this is Ignored in the Upsert (Patch) Method, when using the following request:

PATCH /odata/Records(record_number='123-0001-222')

If we change this in: vendor/flat3/lodata/src/EntitySet.php it works with the alternative key.

original:

    public function negotiateUpsert(PropertyValue $entityId, Transaction $transaction): PipeInterface
    {
        $key = $this->getType()->getKey();

modified:

    public function negotiateUpsert(PropertyValue $entityId, Transaction $transaction): PipeInterface
    {
        $key = $entityId->getProperty();

it seems to ignore the longForm key if specified and use the normal ID instead without this change.

t0815 avatar Jun 05 '25 08:06 t0815