Notes on ScopePort

Rails Migrations: How to define an own column as primary key

by Lennart on Nov.09, 2008, under Ruby On Rails

Usually the Ruby On Rails convention to use ID as the primary key of tables is fine. But sometimes you want to use another column as primary key. In my case I wanted to use the column “pid” of the ScopePort table “health” as the primary key. This is the way to do it:

def self.up
  create_table :health, :primary_key => :pid do |t|
    t.integer :threads, :timestamp
    t.boolean :clienthandler
    t.string :vmem, :packetsOK, :packetsERR
  end
end

The first notable point is the create_table option “:primary_key => :pid”. It instructs rake not to create the ID column but to use the column “pid” as primary key. You don’t need to define :pid later – It will be created automatically as an integer.

Share:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • StumbleUpon
  • Technorati
  • Yigg
  • FriendFeed
  • Identi.ca
  • MySpace
  • TwitThis
:, , , ,

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!