【发布时间】:2011-04-09 04:49:49
【问题描述】:
我有一个关于 Rails 数据库的问题。
- 我应该将“index”添加到“xxx_id”等所有外键吗?
- 我应该在自动创建的“id”列中添加“index”吗?
我应该将“index(unique)”添加到自动创建的“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
到目前为止,答案很好。附加问题。
- 我应该为 xxx_id 添加“唯一索引”,对吗?
【问题讨论】:
标签: mysql ruby-on-rails activerecord