In my Rails application which works with Oracle database (version 11g) I need primary keys to be String
s, not Integer
s. For example, in my Product
model I need primary keys like "p001"
, "p002"
etc.
Asked
Active
Viewed 1,426 times
1

dimakura
- 7,575
- 17
- 36

Debojit Das
- 41
- 6
1 Answers
1
class AddProductWithDifferentPrimaryKey < ActiveRecord:Migration
def change
create_table :table, id: false do |t|
t.string :id, null: false
# other columns
t.timestamps
end
execute "ALTER TABLE table ADD PRIMARY KEY (id);"
end
end
Don't forget to also add this line to your table model so rails knows how to find your new primary key!
class Product < ActiveRecord::Base
self.primary_key = :id
# rest of code
end
Hope this helps. And credit should go to A K H
For more information you can check out his as well as other answers. primary key info
-
Thanks, But how do I add the string sequence to primary key in the rails application itself, or should I create a PLSQL sequence for it. – Debojit Das Sep 15 '15 at 07:54