user365916
user365916

Reputation:

Avoid default activerecord primary index creation

I am trying to workout how to advise/tell activerecord not to create it's primary index by default.

Anyone know how i can achieve this ?

class CreateHouse < ActiveRecord::Migration
 def change
    create_table :houses do |table|
      table.string :name, :null => false, :unique => true
      table.integer :number, :null => false, :unique => true
      table.string :category, :null => false
      table.timestamps(:null => false)
    end
    add_index :houses, [:category, :number], :unique => true
  end
end

THANKS

Upvotes: 0

Views: 85

Answers (1)

vee
vee

Reputation: 38645

You can add id: false to your create_table definition. Try the following:

class CreateHouse < ActiveRecord::Migration
 def change
    create_table :houses, id: false do |table|
      table.string :name, :null => false, :unique => true
      table.integer :number, :null => false, :unique => true
      table.string :category, :null => false
      table.timestamps(:null => false)
    end
    add_index :houses, [:category, :number], :unique => true
  end
end

Update:

Updated create_table and add_index block to use :houses symbol and not :stores as suggested by kengimel in an edit request (which apparently got rejected!).

Upvotes: 2

Related Questions