Tag: column
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.

