# The best way to do primary keys?

**URL:** <https://www.ruby-forum.com/t/the-best-way-to-do-primary-keys/130677>\
**Category:** Rails\
**Created:** [March 1, 2008, 7:09pm UTC](https://www.ruby-forum.com/t/the-best-way-to-do-primary-keys/130677 "2008-03-01T19:09:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [March 1, 2008, 7:09pm UTC](https://www.ruby-forum.com/t/the-best-way-to-do-primary-keys/130677/1 "2008-03-01T19:09:09Z")

</div>

I’ve just made a simple test app - it’s a very basic blog. I ran this  
(or something vaguely like this):  
ruby script/generate scaffold article title:string body:text  
id:primary\_key

Now, I’ve got the scaffold, but it shows an ID field. Is there any  
special shortcut for getting Rails to automatically fill in the primary  
key, or do I have to sort that out manually?

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [March 1, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/the-best-way-to-do-primary-keys/130677/2 "2008-03-01T20:45:01Z")

</div>

Never mind, I’m an idiot. Rails automatically gives objects a primary  
key.

It’s always one step ahead of me; I need to catch up.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [March 1, 2008, 8:53pm UTC](https://www.ruby-forum.com/t/the-best-way-to-do-primary-keys/130677/3 "2008-03-01T20:53:05Z")

</div>

On Mar 1, 2008, at 1:45 PM, Peter B. wrote:

> Never mind, I’m an idiot. Rails automatically gives objects a primary  
> key.

It won’t automatically create an index on it, though, so you might  
consider that.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [March 1, 2008, 9:23pm UTC](https://www.ruby-forum.com/t/the-best-way-to-do-primary-keys/130677/4 "2008-03-01T21:23:15Z")

</div>

On 01 Mar 2008, at 20:52, George B. wrote:

> > Never mind, I’m an idiot. Rails automatically gives objects a primary  
> > key.
> 
> It won’t automatically create an index on it, though, so you might  
> consider that.

The primary key (in mysql at least, but i’d doubt it be otherwise in  
other dbms) is automatically indexed, that’s part of being a primary  
key. No need to put an index on it.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [March 1, 2008, 11:39pm UTC](https://www.ruby-forum.com/t/the-best-way-to-do-primary-keys/130677/5 "2008-03-01T23:39:24Z")

</div>

Peter De Berdt wrote:

> On 01 Mar 2008, at 20:52, George B. wrote:
> 
> > > Never mind, I’m an idiot. Rails automatically gives objects a primary  
> > > key.
> > 
> > It won’t automatically create an index on it, though, so you might  
> > consider that.
> 
> The primary key (in mysql at least, but i’d doubt it be otherwise in  
> other dbms) is automatically indexed, that’s part of being a primary  
> key. No need to put an index on it.
> 
> Best regards
> 
> Peter De Berdt

PostgreSQL and SQL Server (unless it’s changed recently) automatically  
index primary keys, but not foreign keys. In the PostgreSQL docs, I’m  
pretty sure it says that a primary key is enforced via a unique index.

Peace,  
phillip
