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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.