# How to check record duplication before saving?

**URL:** <https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822>\
**Category:** ruby-talk\
**Created:** [11 April 2008 20:36 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822 "2008-04-11T20:36:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_Tsao](https://avatars.discourse-cdn.com/v4/letter/f/0ea827/32.png) [@Frank\_Tsao](https://rubytalk.org/u/Frank_Tsao)\
**Post date:** [11 April 2008 20:36 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/1 "2008-04-11T20:36:40Z")

</div>

Hi, everyone,

I am a beginner in ruby programming and I have a minor question about  
data checking, if someone who could give me a suggestion that would be  
great.

The system needs to prevent a duplicated record happened in database,  
the record is consisted of two fields which are column A and column B.  
Supposing a record has been saved in database that the A=1, B=1.  
Afterward, the same set of record can not be accepted again, for  
example, record: A=1, B=2 and A=2, B=1 both are acceptable. In other  
words, record: A=1, B=1 can not be saved in database.

So how to check and prevent a new record that has the same columns with  
same values tend to be saved in database?

Frank

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Dave\_Mail\_Archive](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Dave\_Mail\_Archive](https://rubytalk.org/u/Dave_Mail_Archive)\
**Post date:** [11 April 2008 20:40 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/2 "2008-04-11T20:40:35Z")

</div>

Try creating a unique database index that spans the two columns.

Regards

Dave

> **···**
>
> On Apr 11, 2008, at 3:36 PM, Frank Tsao wrote:
> 
> > So how to check and prevent a new record that has the same columns with  
> > same values tend to be saved in database?

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)\
**Post date:** [11 April 2008 20:47 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/3 "2008-04-11T20:47:44Z")

</div>

Frank Tsao wrote:

> Hi, everyone,
> 
> I am a beginner in ruby programming and I have a minor question about  
> data checking, if someone who could give me a suggestion that would be  
> great.
> 
> The system needs to prevent a duplicated record happened in database,  
> the record is consisted of two fields which are column A and column B.  
> Supposing a record has been saved in database that the A=1, B=1.  
> Afterward, the same set of record can not be accepted again, for  
> example, record: A=1, B=2 and A=2, B=1 both are acceptable. In other  
> words, record: A=1, B=1 can not be saved in database.
> 
> So how to check and prevent a new record that has the same columns with  
> same values tend to be saved in database?

Most database systems can handle that, be defining a UNIQUE constraint  
on columns.

However, that means that A = 1 and B = 2 won't work.

Different methods to access databases have different ways to handle  
these kinds of collisions, or at least different ways to implement the  
collision detection.

(For example, you can fetch all records, and check if they are already  
set, but that is rather bad for memory consumption and speed, but might  
be enough in your case.)

So, if you can tell us how you access your database (and what it is), we  
can help you out much better.

- --  
Phillip Gawlowski  
Twitter: twitter.com/cynicalryan

~ - You know you've been hacking too long when...  
...your brain keeps hallucinating random "system error: collision with  
stack heap" or "Guru Meditation Mode # Three billion and fifty-two,  
press left button to continue" or even "This is not a DOS disk." error  
messages.

---

<div class="post-metadata">

**Author:** ![Dave\_Mail\_Archive](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Dave\_Mail\_Archive](https://rubytalk.org/u/Dave_Mail_Archive)\
**Post date:** [11 April 2008 21:02 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/4 "2008-04-11T21:02:44Z")

</div>

Sure it will- you define the unique index to span the two columns, and then only non-unoque combinations of those columns will fail.

Dave

> **···**
>
> On Apr 11, 2008, at 3:47 PM, Phillip Gawlowski wrote:
> 
> > Most database systems can handle that, be defining a UNIQUE constraint  
> > on columns.
> > 
> > However, that means that A = 1 and B = 2 won't work.

---

<div class="post-metadata">

**Author:** ![Frank\_Tsao](https://avatars.discourse-cdn.com/v4/letter/f/0ea827/32.png) [@Frank\_Tsao](https://rubytalk.org/u/Frank_Tsao)\
**Post date:** [11 April 2008 23:02 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/5 "2008-04-11T23:02:10Z")

</div>

Dave Thomas wrote:

> **···**
>
> > On Apr 11, 2008, at 3:36 PM, Frank Tsao wrote:
> > 
> > > So how to check and prevent a new record that has the same columns  
> > > with  
> > > same values tend to be saved in database?
> > 
> > Try creating a unique database index that spans the two columns.
> > 
> > Regards
> > 
> > Dave
> 
> Thanks for your help, but the situation in A=1, B=2 or A=2, B=1 that  
> wont be accepted if using unique.
> 
> My point is that when record(A=1, B=1) was existed in database, any new  
> record(A=1, B=1) can not be valided. They can be (A=1, B=2), (A=1, B=3)  
> or (A=5, B=1) etc.  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Frank\_Tsao](https://avatars.discourse-cdn.com/v4/letter/f/0ea827/32.png) [@Frank\_Tsao](https://rubytalk.org/u/Frank_Tsao)\
**Post date:** [11 April 2008 23:10 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/6 "2008-04-11T23:10:46Z")

</div>

Thanks for your help. I just used Instant Rails 2.0 for developing.  
Mongrel and Sqlite.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![RubyTalk](https://avatars.discourse-cdn.com/v4/letter/r/9fc29f/32.png) [@RubyTalk](https://rubytalk.org/u/RubyTalk)\
**Post date:** [11 April 2008 21:46 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/7 "2008-04-11T21:46:19Z")

</div>

If you are using Rails and migrations

add\_index(:table, [:column1, :column2], :unique =\> true)

if not something like this sql

CREATE UNIQUE INDEX index\_name ON table(column1,column2);

Becker

> **···**
>
> On Fri, Apr 11, 2008 at 2:02 PM, Dave Thomas \<dave@pragprog.com\> wrote:
> 
> > On Apr 11, 2008, at 3:47 PM, Phillip Gawlowski wrote:
> > 
> > \> Most database systems can handle that, be defining a UNIQUE constraint  
> > \> on columns.  
> > \>  
> > \> However, that means that A = 1 and B = 2 won't work.  
> > \>
> > 
> > Sure it will- you define the unique index to span the two columns, and then  
> > only non-unoque combinations of those columns will fail.
> > 
> > Dave

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)\
**Post date:** [11 April 2008 23:19 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/8 "2008-04-11T23:19:40Z")

</div>

Frank Tsao wrote:

> Thanks for your help. I just used Instant Rails 2.0 for developing.  
> Mongrel and Sqlite.

As far as I can see, you have to iterate of all the present records to  
check for uniqueness of values, since SQLite3 doesn't allow for a  
multi-column uniqueness.

With MySQl and other RDBMSes, Dave Thomas' idea should work (I don't  
know, since I never had to handle this situation yet myself).

- --  
Phillip Gawlowski  
Twitter: twitter.com/cynicalryan

Rule of Open-Source Programming #13:

Your first release can always be improved upon.

---

<div class="post-metadata">

**Author:** ![Rick\_Tessner](https://avatars.discourse-cdn.com/v4/letter/r/82dd89/32.png) [@Rick\_Tessner](https://rubytalk.org/u/Rick_Tessner)\
**Post date:** [12 April 2008 22:08 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/9 "2008-04-12T22:08:55Z")

</div>

Hi,

Excerpts from shuhao.tsao's message of Fri Apr 11 16:02:10 -0700 2008:

> Dave Thomas wrote:  
> \>\> So how to check and prevent a new record that has the same columns  
> \>\> with  
> \>\> same values tend to be saved in database?  
> \>  
> \> Try creating a unique database index that spans the two columns.  
> \>  
> \>  
> \> Regards  
> \>  
> \>  
> \> Dave
> 
> Thanks for your help, but the situation in A=1, B=2 or A=2, B=1 that  
> wont be accepted if using unique.
> 
> My point is that when record(A=1, B=1) was existed in database, any new  
> record(A=1, B=1) can not be valided. They can be (A=1, B=2), (A=1, B=3)  
> or (A=5, B=1) etc.

You could try creating a third column. Let's suppose that you know that  
all A & B values are 6 digits or less, create a unique string column  
that will then contain the value of A & B sorted, zero padded and  
joined.

&nbsp;&nbsp;u = [a, b].sort.map { |i| "%06d" %i }.join

So, the database would contain 3 columns: a, b, u with u being defined  
as unique in the database.

So, with a = 1; b = 2 we get u = "000001000002"

or with a = 2; b = 1 we also get u = "000001000002"

> **···**
>
> > \> On Apr 11, 2008, at 3:36 PM, Frank Tsao wrote:
> 
> --  
> Rick Tessner  
> rick.tessner@gmail.com

---

<div class="post-metadata">

**Author:** ![Frank\_Tsao](https://avatars.discourse-cdn.com/v4/letter/f/0ea827/32.png) [@Frank\_Tsao](https://rubytalk.org/u/Frank_Tsao)\
**Post date:** [11 April 2008 23:01 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/10 "2008-04-11T23:01:18Z")

</div>

RubyTalk@gmail.com wrote:

> If you are using Rails and migrations
> 
> add\_index(:table, [:column1, :column2], :unique =\> true)
> 
> if not something like this sql
> 
> CREATE UNIQUE INDEX index\_name ON table(column1,column2);
> 
> Becker

Thanks for your help, but the situation in A=1, B=2 or A=2, B=1 that  
wont be accepted if using unique.

My point is that when record(A=1, B=1) was existed in database, any new  
record(A=1, B=1) can not be valided. They can be (A=1, B=2), (A=1, B=3)  
or (A=5, B=1) etc.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [11 April 2008 23:43 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/11 "2008-04-11T23:43:59Z")

</div>

You probably have a primary key constraint on one of the columns  
(unless you are using kindergarten SQLite). Example without correct  
caps...

create table stuff (  
&nbsp;&nbsp;colA int not null,  
&nbsp;&nbsp;colB int not null,  
&nbsp;&nbsp;primary key (colA, colB)  
)

...or if you succumb to the idea integer IDs for the primary key are a  
good idea...

create table stuff (  
&nbsp;&nbsp;id int not null primary key,  
&nbsp;&nbsp;colA int not null,  
&nbsp;&nbsp;colB int not null,  
&nbsp;&nbsp;unique (colA, colB)  
)

Not tested.

Todd

> **···**
>
> On Fri, Apr 11, 2008 at 6:01 PM, Frank Tsao \<shuhao.tsao@gmail.com\> wrote:
> 
> > RubyTalk@gmail.com wrote:  
> > \> If you are using Rails and migrations  
> > \>  
> > \> add\_index(:table, [:column1, :column2], :unique =\> true)  
> > \>  
> > \> if not something like this sql  
> > \>  
> > \> CREATE UNIQUE INDEX index\_name ON table(column1,column2);  
> > \>  
> > \> Becker
> > 
> > Thanks for your help, but the situation in A=1, B=2 or A=2, B=1 that  
> > wont be accepted if using unique.
> > 
> > My point is that when record(A=1, B=1) was existed in database, any new  
> > record(A=1, B=1) can not be valided. They can be (A=1, B=2), (A=1, B=3)  
> > or (A=5, B=1) etc.

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [12 April 2008 16:34 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/12 "2008-04-12T16:34:28Z")

</div>

> You probably have a primary key constraint on one of the columns  
> (unless you are using kindergarten SQLite).

That probably sounded bad. Sorry.

> Example without correct  
> caps...
> 
> create table stuff (  
> &nbsp;&nbsp;colA int not null,  
> &nbsp;&nbsp;colB int not null,  
> &nbsp;&nbsp;primary key (colA, colB)  
> )
> 
> ...or if you succumb to the idea integer IDs for the primary key are a  
> good idea...
> 
> create table stuff (  
> &nbsp;&nbsp;id int not null primary key,  
> &nbsp;&nbsp;colA int not null,  
> &nbsp;&nbsp;colB int not null,  
> &nbsp;&nbsp;unique (colA, colB)  
> )

In rails, I should point out I \_have\_ to use the integer ID for my  
primary key, which irks me to no end because it goes against  
everything I know about set theory. That whining aside, I might try  
to fix it, but the underlying rails code is intimidating.

Todd

> **···**
>
> On Fri, Apr 11, 2008 at 6:43 PM, Todd Benson \<caduceass@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![RubyTalk](https://avatars.discourse-cdn.com/v4/letter/r/9fc29f/32.png) [@RubyTalk](https://rubytalk.org/u/RubyTalk)\
**Post date:** [12 April 2008 19:27 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/13 "2008-04-12T19:27:04Z")

</div>

I like to use guids for ids.

create\_table(:name,{:id=\>false}) do |t|  
&nbsp;&nbsp;&nbsp;t.column(:id,:string,:limit=\>64)  
end  
execute(sql\_statement\_to\_create\_primary\_key)

Becker

> **···**
>
> On Sat, Apr 12, 2008 at 9:34 AM, Todd Benson \<caduceass@gmail.com\> wrote:
> 
> > On Fri, Apr 11, 2008 at 6:43 PM, Todd Benson \<caduceass@gmail.com\> wrote:
> > 
> > \> You probably have a primary key constraint on one of the columns  
> > \> (unless you are using kindergarten SQLite).
> > 
> > That probably sounded bad. Sorry.
> > 
> > \> Example without correct  
> > \> caps...  
> > \>  
> > \> create table stuff (  
> > \> colA int not null,  
> > \> colB int not null,  
> > \> primary key (colA, colB)  
> > \> )  
> > \>  
> > \> ...or if you succumb to the idea integer IDs for the primary key are a  
> > \> good idea...  
> > \>  
> > \> create table stuff (  
> > \> id int not null primary key,  
> > \> colA int not null,  
> > \> colB int not null,  
> > \> unique (colA, colB)  
> > \> )
> > 
> > In rails, I should point out I \_have\_ to use the integer ID for my  
> > primary key, which irks me to no end because it goes against  
> > everything I know about set theory. That whining aside, I might try  
> > to fix it, but the underlying rails code is intimidating.
> > 
> > Todd

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [12 April 2008 20:55 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/14 "2008-04-12T20:55:26Z")

</div>

no db actually deals with sets - try to do a query and get the results back in different order each time. in practice they are \*ordered\* tuples of key value pairs and every db i've used is written with this implicit understanding. having an id is no different that having an 'updated\_at' field - it simply provides a total ordering which is arbitrary. fighting the id is just swimming upstream to nowhere - i know as i did it for a long time. 😉

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Apr 12, 2008, at 10:34 AM, Todd Benson wrote:
> 
> > In rails, I should point out I \_have\_ to use the integer ID for my  
> > primary key, which irks me to no end because it goes against  
> > everything I know about set theory. That whining aside, I might try  
> > to fix it, but the underlying rails code is intimidating.
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [13 April 2008 00:14 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/15 "2008-04-13T00:14:32Z")

</div>

I understand that, for example, postgresql and mysql have labeled all  
things with an integer. But, in the table itself, umm, doesn't make  
much sense...

create table my\_set (  
&nbsp;&nbsp;id int not null primary key,  
&nbsp;&nbsp;a int,  
&nbsp;&nbsp;b int,  
);

insert into my\_set values (1, 1, 1);

insert into my\_set values (2, 1, 1);

It gets even worse with built in functions like auto\_increment, where  
you've tossed all identity of the tuple to the wind (think of moving  
such a table to another db).

I'm still a firm believer in using logical primary keys instead of  
arbitrary ones anyway, even if it takes a little more effort, but I'm  
willing to listen to other ways 🙂

Todd

> **···**
>
> On Sat, Apr 12, 2008 at 3:55 PM, ara.t.howard \<ara.t.howard@gmail.com\> wrote:
> 
> > On Apr 12, 2008, at 10:34 AM, Todd Benson wrote:
> > 
> > \>  
> > \> In rails, I should point out I \_have\_ to use the integer ID for my  
> > \> primary key, which irks me to no end because it goes against  
> > \> everything I know about set theory. That whining aside, I might try  
> > \> to fix it, but the underlying rails code is intimidating.  
> > \>
> > 
> > no db actually deals with sets - try to do a query and get the results back  
> > in different order each time. in practice they are \*ordered\* tuples of key  
> > value pairs and every db i've used is written with this implicit  
> > understanding. having an id is no different that having an 'updated\_at'  
> > field - it simply provides a total ordering which is arbitrary. fighting  
> > the id is just swimming upstream to nowhere - i know as i did it for a long  
> > time. 😉
> > 
> > a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [13 April 2008 01:37 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/16 "2008-04-13T01:37:38Z")

</div>

I'll clarify a little (I may have told this little story before). I  
once worked for a company that had complete confidence in its ERP  
database (to be nameless). Suddenly, one day, there's about a half  
mil missing for no real reason except for what the reports said.  
Having been embezzled before, they wanted to find the culprit. The  
culprit turned out to be the database schema (and they were \_so\_ ready  
for a witch hunt).

Todd

> **···**
>
> On Sat, Apr 12, 2008 at 7:14 PM, Todd Benson \<caduceass@gmail.com\> wrote:
> 
> > On Sat, Apr 12, 2008 at 3:55 PM, ara.t.howard \<ara.t.howard@gmail.com\> wrote:  
> > \>  
> > \> On Apr 12, 2008, at 10:34 AM, Todd Benson wrote:  
> > \>  
> > \> \>  
> > \> \> In rails, I should point out I \_have\_ to use the integer ID for my  
> > \> \> primary key, which irks me to no end because it goes against  
> > \> \> everything I know about set theory. That whining aside, I might try  
> > \> \> to fix it, but the underlying rails code is intimidating.  
> > \> \>  
> > \>  
> > \> no db actually deals with sets - try to do a query and get the results back  
> > \> in different order each time. in practice they are \*ordered\* tuples of key  
> > \> value pairs and every db i've used is written with this implicit  
> > \> understanding. having an id is no different that having an 'updated\_at'  
> > \> field - it simply provides a total ordering which is arbitrary. fighting  
> > \> the id is just swimming upstream to nowhere - i know as i did it for a long  
> > \> time. 😉  
> > \>  
> > \> a @ [http://codeforpeople.com/](http://codeforpeople.com/)
> > 
> > I understand that, for example, postgresql and mysql have labeled all  
> > things with an integer. But, in the table itself, umm, doesn't make  
> > much sense...
> > 
> > create table my\_set (
> > 
> > &nbsp;&nbsp;id int not null primary key,  
> > &nbsp;&nbsp;a int,  
> > &nbsp;&nbsp;b int,  
> > );
> > 
> > insert into my\_set values (1, 1, 1);
> > 
> > insert into my\_set values (2, 1, 1);
> > 
> > It gets even worse with built in functions like auto\_increment, where  
> > you've tossed all identity of the tuple to the wind (think of moving  
> > such a table to another db).
> > 
> > I'm still a firm believer in using logical primary keys instead of  
> > arbitrary ones anyway, even if it takes a little more effort, but I'm  
> > willing to listen to other ways 🙂

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [13 April 2008 02:01 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/17 "2008-04-13T02:01:46Z")

</div>

yeah that's valid. i have at least one db where every id is a globally unique uuid - and by globally i mean according to rfc. still

olddb.id

is a valid concept...

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Apr 12, 2008, at 6:14 PM, Todd Benson wrote:
> 
> > It gets even worse with built in functions like auto\_increment, where  
> > you've tossed all identity of the tuple to the wind (think of moving  
> > such a table to another db).
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [13 April 2008 01:48 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/18 "2008-04-13T01:48:23Z")

</div>

Wow. That sounded incriminating, didn't it! My apologies for the  
noise, but it's true. The company I worked for lost a bunch of money  
because of a culmination of practices. They were paying for new part  
revs and the db didn't reflect it. "Bad" shipments were dropped/lost  
in the shipping part of the database. Also, it was full of tables  
called: 12400010, 12400011, etc. and the relations were not even  
close to sane. The "help" was paid about $1000/hr to set it up.

Todd

> **···**
>
> On Sat, Apr 12, 2008 at 8:37 PM, Todd Benson \<caduceass@gmail.com\> wrote:
> 
> > On Sat, Apr 12, 2008 at 7:14 PM, Todd Benson \<caduceass@gmail.com\> wrote:  
> > \> On Sat, Apr 12, 2008 at 3:55 PM, ara.t.howard \<ara.t.howard@gmail.com\> wrote:  
> > \> \>  
> > \> \> On Apr 12, 2008, at 10:34 AM, Todd Benson wrote:  
> > \> \>  
> > \> \> \>  
> > \> \> \> In rails, I should point out I \_have\_ to use the integer ID for my  
> > \> \> \> primary key, which irks me to no end because it goes against  
> > \> \> \> everything I know about set theory. That whining aside, I might try  
> > \> \> \> to fix it, but the underlying rails code is intimidating.  
> > \> \> \>  
> > \> \>  
> > \> \> no db actually deals with sets - try to do a query and get the results back  
> > \> \> in different order each time. in practice they are \*ordered\* tuples of key  
> > \> \> value pairs and every db i've used is written with this implicit  
> > \> \> understanding. having an id is no different that having an 'updated\_at'  
> > \> \> field - it simply provides a total ordering which is arbitrary. fighting  
> > \> \> the id is just swimming upstream to nowhere - i know as i did it for a long  
> > \> \> time. 😉  
> > \> \>  
> > \> \> a @ [http://codeforpeople.com/](http://codeforpeople.com/)  
> > \>  
> > \> I understand that, for example, postgresql and mysql have labeled all  
> > \> things with an integer. But, in the table itself, umm, doesn't make  
> > \> much sense...  
> > \>  
> > \> create table my\_set (  
> > \>  
> > \> id int not null primary key,  
> > \> a int,  
> > \> b int,  
> > \> );  
> > \>  
> > \> insert into my\_set values (1, 1, 1);  
> > \>  
> > \> insert into my\_set values (2, 1, 1);  
> > \>  
> > \> It gets even worse with built in functions like auto\_increment, where  
> > \> you've tossed all identity of the tuple to the wind (think of moving  
> > \> such a table to another db).  
> > \>  
> > \> I'm still a firm believer in using logical primary keys instead of  
> > \> arbitrary ones anyway, even if it takes a little more effort, but I'm  
> > \> willing to listen to other ways 🙂
> > 
> > I'll clarify a little (I may have told this little story before). I  
> > once worked for a company that had complete confidence in its ERP  
> > database (to be nameless). Suddenly, one day, there's about a half  
> > mil missing for no real reason except for what the reports said.  
> > Having been embezzled before, they wanted to find the culprit. The  
> > culprit turned out to be the database schema (and they were \_so\_ ready  
> > for a witch hunt).
> > 
> > Todd

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [13 April 2008 02:02 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/19 "2008-04-13T02:02:50Z")

</div>

guess we have something to learn from them! 😉

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Apr 12, 2008, at 7:48 PM, Todd Benson wrote:
> 
> > The "help" was paid about $1000/hr to set it up.
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [13 April 2008 02:55 UTC](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822/20 "2008-04-13T02:55:21Z")

</div>

Yeah, it's funny. That may sound like an exaggeration, but after you  
add up the flight cost and motel stay, it probably was more like  
$1200/hr. The guy worked about 4 hours per day for a total of 3 or 4  
days.

> **···**
>
> On Sat, Apr 12, 2008 at 9:02 PM, ara.t.howard \<ara.t.howard@gmail.com\> wrote:
> 
> > On Apr 12, 2008, at 7:48 PM, Todd Benson wrote:
> > 
> > \> The "help" was paid about $1000/hr to set it up.  
> > \>
> > 
> > guess we have something to learn from them! 😉

[Next page](https://rubytalk.org/t/how-to-check-record-duplication-before-saving/45822.md?page=2)
