最佳答案
我有一个关于 Rails 数据库的问题。
我应该添加“索引(唯一)”到自动创建的“ id”列?
如果我同时向两个外键添加索引(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
Great answer so far. Additional question.