active_record_upsert icon indicating copy to clipboard operation
active_record_upsert copied to clipboard

Updates to an upserted record are not applied

Open bgerami opened this issue 5 years ago • 0 comments

After upserting, the very next save on the resulting record does not persist

MyModel.find_by(uuid: 1234) # nil

record = MyModel.new # (id, uuid, name, age)
record.uuid = 1234 # uuid is the upsert_key
result = record.upsert # creates record with uuid 1234

result.update(name: "foo")

persisted = MyModel.find_by(uuid: 1234)
persisted.name # nil

result.update(age: 42)

persisted = MyModel.find_by(uuid: 1234)
persisted.age # 42

The first upsert does an INSERT ON CONFLICT generating the record The very next update tries to update every column UPDATE "my_model" SET "id" = $1, "name" = $2 ... Then the update after that does the usual UPDATE "my_model" SET "age" = $1 ...

bgerami avatar Jun 06 '19 21:06 bgerami