jesjos / active_record_upsert

Upsert for Rails 5 / Active Record 5
MIT License
207 stars 51 forks source link

Columns with database defaults are not updated #98

Open sjorsbranderhorst opened 5 years ago

sjorsbranderhorst commented 5 years ago

We found that when

1) having a default in the database (here for a table called hardwares)

┌─────────┬─────────────────┬────────
│ Column  │           Type  │ Default                       
├─────────┼─────────────────┼─────────
│ id      │ integer         │ ...
│ prio    │ integer         │ 999

2) having an existing entry in the database with a non default value for prio like: 998 with id: 1

That upserting like so:

hw = { id: 1, prio: 999 }
id = Hardware.new(prio: hw[:prio]).upsert.id

won't mention the prio column in the ON CONFLICT part resulting in no update. However, when upserting like so:

id = Hardware.upsert(prio: hw[:prio]).id

did indeed update the record in the database (back to its default value). Am not sure if this is (a bug/an issue), but thought I would drop this here. Thanks for all the work on the nice Gem!

olleolleolle commented 5 years ago

Thanks for explaining!

Could you make the above into a part of our README - as a Pull Request? It's a sort of workaround/explanation of behavior.