active_record_upsert icon indicating copy to clipboard operation
active_record_upsert copied to clipboard

Columns with database defaults are not updated

Open sjorsbranderhorst opened this issue 5 years ago • 1 comments

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!

sjorsbranderhorst avatar Oct 29 '19 16:10 sjorsbranderhorst

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.

olleolleolle avatar Oct 29 '19 16:10 olleolleolle