-4

When creating a table on my pgslq db with rake, rails allways create an "id" field as primary key.

My issue is that i need to have the id field with a custom name. How can i specify the database fields with ONLY the fields i want, specifying the primary key?

Eric S
  • 1,363
  • 1
  • 10
  • 20
Techmago
  • 380
  • 4
  • 18
  • 4
    This is covered in [many](http://stackoverflow.com/questions/9986658/setting-primary-key-for-a-database-not-named-id) [other](http://stackoverflow.com/questions/1200568/using-rails-how-can-i-set-my-primary-key-to-not-be-an-integer-typed-column) Stack Overflow questions and [all over the web](http://roninonrails.blogspot.com/2008/06/using-non-standard-primary-keys-with.html), but I'll just add that it's going to make your life hell, and you should do everything in your power to switch to using `id` in your tables if possible. It's a major fault of Rails, but `id` makes things Just Work. – Jim Stewart Mar 27 '13 at 13:24
  • Downvote for not searching SO before posting a redundant question. – Jim Stewart Mar 27 '13 at 13:25
  • I did a quick search about it and failed to found anything. Thanx for the extra info about the problem of not using the ID with the default name... The problem was because the data model we have, now we are re discussing about it. – Techmago Mar 27 '13 at 14:00

1 Answers1

3

You can specify primary key passing its name to create_table in migration:

create_table :my_table, :primary_key => :custom_id do |t|
  #...
end 
Ilya Khokhryakov
  • 3,686
  • 1
  • 19
  • 22