loopback icon indicating copy to clipboard operation
loopback copied to clipboard

upsert doesn't catch errors

Open ghost opened this issue 10 years ago • 6 comments

The upsert() method doesn't catch errors (duplicate key, foreign key, ...). The callback doesn't get error object, and gets the "updated" model instance. This happens in both cases (update or insert). I'm using MySQL.

ghost avatar Jul 30 '15 08:07 ghost

"options": {
  "validateUpsert": true
}

Do you have this in your model settings?

ritch avatar Aug 06 '15 22:08 ritch

Thanks. Yes, I have it. The property validateUpsert ensure that upsert() returns an error when validation fails (http://docs.strongloop.com/display/public/LB/Model+definition+JSON+file). What I mean the errors (duplicate key, foreign key, ...) are fired from the database operation, after the model validation.

ghost avatar Aug 07 '15 07:08 ghost

Ah... well validation can include validating uniqueness.

As far as errors thrown from the database, @raymondfeng do you have any thoughts? Missing an error thrown from the driver sounds like a large issue.

ritch avatar Aug 07 '15 18:08 ritch

Upsert fails silently when float number is sent to column defined as number with postgresql. I hope this example shows it. I have Strongloop model definition json file with column:

"score": {
  "type": "number"
}

And I send PUT request with url /model (or /model/:id) containing body:

{
  "id":"1",
  "score": 123.123
}

The response will have status code 200 and the response body will have the updated score, but A GET request shows that the model got never updated.

I can fix this by updating my model definition:

"score": {
  "type": "number",
  "postgresql": {
    "columnName": "score",
    "dataType": "numeric",
    "dataLength": null,
    "dataPrecision": 10,
    "dataScale": 5,
    "nullable": "NO"
  }
}

Only catching and communicating this to my 3rd party users is tricky because they will assume everything worked fine.

viljami avatar Dec 16 '15 02:12 viljami

Just tripped into this as well. In my case trying to upsert when violating a unique key constraint is failing silently.

elropero avatar Aug 05 '16 14:08 elropero

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 21 '21 05:07 stale[bot]