# Avoiding duplicate inserts from multiple ruby processes

**URL:** <https://www.ruby-forum.com/t/avoiding-duplicate-inserts-from-multiple-ruby-processes/82462>\
**Category:** Rails\
**Created:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/avoiding-duplicate-inserts-from-multiple-ruby-processes/82462 "2007-01-19T16:30:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bataras](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ab097/32.png) [@bataras](https://www.ruby-forum.com/u/bataras)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/avoiding-duplicate-inserts-from-multiple-ruby-processes/82462/1 "2007-01-19T16:30:37Z")

</div>

How do I use ActiveRecord to safely add-or-update the same unique row  
in a table from multiple ruby processes?

Suppose I have a table called People with a column called ‘name’. In  
the DB (which is MySql), the name column has a unique index on it.

Now, 2 ruby processes are chugging along adding rows in the People  
table. These processes may (will) come across both new and existing  
rows in the People table. I need to make sure that I don’t trigger a  
“Mysql::Error: #23000Duplicate entry” error on the name column.

---

<div class="post-metadata">

**Author:** ![bataras](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ab097/32.png) [@bataras](https://www.ruby-forum.com/u/bataras)\
**Post date:** [September 25, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/avoiding-duplicate-inserts-from-multiple-ruby-processes/82462/2 "2007-09-25T22:41:45Z")

</div>

bataras wrote:

> Suppose I have a table called People with a column called ‘name’. In  
> the DB (which is MySql), the name column has a unique index on it.
> 
> Now, 2 ruby processes are chugging along adding rows in the People  
> table. These processes may (will) come across both new and existing  
> rows in the People table. I need to make sure that I don’t trigger a  
> “Mysql::Error: #23000Duplicate entry” error on the name column.

If the updates do not depend on the current contents of the rows,  
use REPLACE: [http://dev.mysql.com/doc/refman/5.0/en/replace.html](http://dev.mysql.com/doc/refman/5.0/en/replace.html) .  
Otherwise use INSERT … ON DUPLICATE KEY UPDATE:  
[http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html](http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html) .

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![bataras](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ab097/32.png) [@bataras](https://www.ruby-forum.com/u/bataras)\
**Post date:** [September 25, 2007, 10:44pm UTC](https://www.ruby-forum.com/t/avoiding-duplicate-inserts-from-multiple-ruby-processes/82462/3 "2007-09-25T22:44:02Z")

</div>

thanks. and thanks for the links too. What i actually want to do from  
each process is insert the row if it doesn’t exist, but if it does,  
don’t change it, but select it (or otherwise keep it locked in the  
current transaction) while I do some other processing related to it.
