Forum: Ruby on Rails Migration: How to make a column the primary key

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3504745159d79c585e4f7df8df76aa10?d=identicon&s=25 Ingo Weiss (ingoweiss)
on 2006-04-24 14:38
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
8716cce9a7b721786e5425b8cc1f9951?d=identicon&s=25 sean lynch (Guest)
on 2006-04-24 15:59
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 Weiss 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
13ea48f056b19349027d2f4a6921a46a?d=identicon&s=25 Jean-François (Guest)
on 2006-04-24 16:04
(Received via mailing list)
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.
3504745159d79c585e4f7df8df76aa10?d=identicon&s=25 Ingo Weiss (ingoweiss)
on 2006-04-24 17:39
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 Weiss
This topic is locked and can not be replied to.