# Re: update\_all Not Incrementing lock\_version

**URL:** <https://www.ruby-forum.com/t/re-update-all-not-incrementing-lock-version/98794>\
**Category:** Rails\
**Created:** [July 18, 2006, 7:30am UTC](https://www.ruby-forum.com/t/re-update-all-not-incrementing-lock-version/98794 "2006-07-18T07:30:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chuck\_Simmons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@Chuck\_Simmons](https://www.ruby-forum.com/u/Chuck_Simmons)\
**Post date:** [July 18, 2006, 7:30am UTC](https://www.ruby-forum.com/t/re-update-all-not-incrementing-lock-version/98794/1 "2006-07-18T07:30:21Z")

</div>

Don –

"  
UPDATE jobs SET jobs.running = 1 WHERE (id in (10,11,12))  
…  
UPDATE jobs  
SET jobs.running = 1  
WHERE (id in (10,11,12)  
AND lock\_version in (1340, 1333, 807));  
"

I think you want

update jobs set jobs.running = 1, jobs.lock\_version = jobs.lock\_version

- 1  
where (id = 10 and lock\_version = 1340)  
or (id = 11 and lock\_version = 1333)  
or (id = 12 and lock\_version = 807)

With a begin transaction somewhere and a rollback if this doesn’t update  
3 rows.

Others and Don –

So, I have a record with a lock\_version field and Rails did not  
increment the lock\_version field prior to saving the record on disk.  
The ‘save’ method does not maintain lock\_version.

I don’t understand what the point is in having magic fields if they are  
going to work magically.

Chuck

---

<div class="post-metadata">

**Author:** ![Chuck\_Simmons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@Chuck\_Simmons](https://www.ruby-forum.com/u/Chuck_Simmons)\
**Post date:** [July 18, 2006, 9:24am UTC](https://www.ruby-forum.com/t/re-update-all-not-incrementing-lock-version/98794/2 "2006-07-18T09:24:13Z")

</div>

Chuck Simmons wrote:

> I think you want
> 
> update jobs set jobs.running = 1, jobs.lock\_version = jobs.lock\_version
> 
> - 1  
> where (id = 10 and lock\_version = 1340)  
> or (id = 11 and lock\_version = 1333)  
> or (id = 12 and lock\_version = 807)
> 
> With a begin transaction somewhere and a rollback if this doesn’t update  
> 3 rows.

Chuck,

Yes. Thank you. You are exactly right. I was so focused on setting  
the running field I completely missed the part of the update that was  
part and parcel to post!

Thank you for the correction!

Wouldn’t this also work?

UPDATE jobs  
SET jobs.running = 1, jobs.lock\_version = jobs.lock\_version + 1  
WHERE (id in (10,11,12)  
AND lock\_version in (1340, 1333, 807));

If so I guess it’s a matter of which is more efficient. Like I  
mentioned. I’m certainly not a SQL guy! If any SQL gurus out there can  
provide a litte guidence here and the Rails community can verify that  
update\_all should increment lock\_version, then we can work on a patch.

- Don
