active_record_upsert
active_record_upsert copied to clipboard
Columns with database defaults are not updated
We found that when
-
having a default in the database (here for a table called
hardwares
)┌─────────┬─────────────────┬──────── │ Column │ Type │ Default
├─────────┼─────────────────┼───────── │ id │ integer │ ... │ prio │ integer │ 999 -
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!
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.