Reputation: 43
I am trying to create custom primary key in rails 6, but didn't created primary key, it's created normal id.
migration i have given
def change
create_table(:m_user_status, :id => false) do |t|
t.integer :id, :options => 'PRIMARY KEY'
t.string :status_name, limit: 20
t.integer :status_check
t.timestamps
end
from above just created id with int4 type, but i want create id with type int4 and primary key without auto-increment. how i give id without auto-incremented and type int4(integer normal)
Upvotes: 1
Views: 2151
Reputation: 2495
You can combine the :id
and :primary_key
options according to the docs here: https://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/create_table
For example:
class CreatedummyTable < ActiveRecord::Migration[6.0]
def change
create_table :dummy_table, id: :int4, primary_key: :foo do |t|
t.timestamps
end
end
end
Creates this schema in postgres:
create_table "dummy_table", primary_key: "foo", id: :integer, default: nil, force: :cascade do |t|
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
Upvotes: 6