# Migration: How to make a column the primary key

**URL:** <https://www.ruby-forum.com/t/migration-how-to-make-a-column-the-primary-key/58647>\
**Category:** Rails\
**Created:** [April 24, 2006, 2:38pm UTC](https://www.ruby-forum.com/t/migration-how-to-make-a-column-the-primary-key/58647 "2006-04-24T14:38:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [April 24, 2006, 2:38pm UTC](https://www.ruby-forum.com/t/migration-how-to-make-a-column-the-primary-key/58647/1 "2006-04-24T14:38:52Z")

</div>

Hi,

I would like to write a migration that creates a table without  
auto-generating a primary key “id” column, and then make a  
integer-column of my choice the primary key.

How can I do this?

Ingo

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [April 24, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/migration-how-to-make-a-column-the-primary-key/58647/2 "2006-04-24T15:59:38Z")

</div>

I’m not sure why you would want to do this, because you did not explain  
your need, but I would advise against it.

If you have some legacy integer data (customer id or employee id) that  
is a primary key in some legacy application, I would not use it as a key  
in a rails app.

Too much ‘magic’ in rails depends on the rails generated id being the  
primary key. You will lose to many productive features if you try to  
change this.

However, you can work with the rails style id and a legacy primary key,  
if you think about your design a little.

I would create two tables, each with their own rails generated self  
incrementing id.

One table would have two columns. The rails generated id, and your  
legacy integer primary key.

The second table should have its rails generated id, and all of the  
other information you want.

Then join the two tables in a one to one relation with a third table  
that contains the two rails genrated id’s from the the joined tables.

This is all covered pretty well in the agile book. Just think of your  
old primary key as data. Like you were looking up a customer or product  
by name, color, or ‘legacy id’.

You can then get all of the rails magic working for you, and use your  
legacy key to look up information easily.

If your data base engine allows it, you can even index the table with  
your legacy key on the legacy key for faster look up. Or write a stored  
procedure, or whatever is the best approach in your db engine.

But If you choose to work against ‘the rails way’ you will find it a  
hard uphill battle.

Good Luck,

Sean

Ingo W. wrote:

> Hi,
> 
> I would like to write a migration that creates a table without  
> auto-generating a primary key “id” column, and then make a  
> integer-column of my choice the primary key.
> 
> How can I do this?
> 
> Ingo

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [April 24, 2006, 4:04pm UTC](https://www.ruby-forum.com/t/migration-how-to-make-a-column-the-primary-key/58647/3 "2006-04-24T16:04:23Z")

</div>

Hello Ingo,

> I would like to write a migration that creates a table without  
> auto-generating a primary key “id” column, and then make a  
> integer-column of my choice the primary key.

def self.up  
create\_table(:foos, :primary\_key =\> ‘bar\_id’) do |t|  
t.column :baz, :string  
# …  
end  
end

```
-- Jean-FranÃ§ois.
```

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [April 24, 2006, 5:39pm UTC](https://www.ruby-forum.com/t/migration-how-to-make-a-column-the-primary-key/58647/4 "2006-04-24T17:39:33Z")

</div>

sean lynch wrote:

> I’m not sure why you would want to do this, because you did not explain  
> your need, but I would advise against it.

Thanks for the advice, Sean!

The reasaon is that the records I am working with already has a unique  
ID number, and I naively thought that using it as the primary key might  
actually make things easier to understand and use.

For example, I might need to clear and reload the data in the table,  
which, if I would go the “Rails way”, would result in the records having  
IDs different from the ones they had before, which of course would break  
associations. But I guess I need to avoid reloading the data in this way  
then.

Ingo W.
