У меня вопрос по базе данных Rails.
Должен ли я добавить «index (unique)» в автоматически созданный столбец «id»?
Если я добавлю index сразу к двум внешним ключам (add_index (:users, [:category, :state_id])
, что произойдет? Чем это отличается от добавления индекса для каждого ключа?
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.integer :category_id
t.integer :state_id
t.string :email
t.boolean :activated
t.timestamps
end
# Do I need this? Is it meaningless to add the index to the primary key?
# If so, do I need :unique => true ?
add_index :users, :id
# I don't think I need ":unique => true here", right?
add_index :users, :category_id # Should I need this?
add_index :users, :state_id # Should I need this?
# Are the above the same as the following?
add_index (:users, [:category, :state_id])
end
end
Отличный ответ. Дополнительный вопрос.