1

In my Rails application which works with Oracle database (version 11g) I need primary keys to be Strings, not Integers. For example, in my Product model I need primary keys like "p001", "p002" etc.

dimakura
  • 7,575
  • 17
  • 36

1 Answers1

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

Community
  • 1
  • 1
ALLCAPZ
  • 81
  • 5
  • 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