Ruby-on-rails 将_index 添加到数据库的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6242304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to add_index to database
提问by LearningRoR
I have the following two migrations already in my database:
我的数据库中已经有以下两个迁移:
When I created Prices:
当我创建价格时:
class CreatePrices < ActiveRecord::Migration
def self.up
create_table :prices do |t|
t.string :price_name
t.decimal :price
t.date :date
t.timestamps
end
# add_index :prices (not added)
end
def self.down
drop_table :prices
end
end
and when I added a user_id to Prices:
当我将 user_id 添加到价格时:
class AddUserIdToPrices < ActiveRecord::Migration
def self.up
add_column :prices, :user_id, :integer
end
# add_index :user_id (not added)
end
def self.down
remove_column :prices, :user_id
end
end
Is there a way from the command line to add prices and user_id to index? I looked at this questionand still was confused on how to go about adding indexes and the parts where I put "not added" seem like they would be error-prone because they were earlier migrations.
有没有办法从命令行将价格和 user_id 添加到索引?我看了这个问题,仍然对如何添加索引感到困惑,我放置“未添加”的部分似乎容易出错,因为它们是较早的迁移。
My question is, what is the best way for me to add indexing to prices and user_id?
我的问题是,为价格和 user_id 添加索引的最佳方法是什么?
Thank you for the help!
感谢您的帮助!
回答by Mikhail Nikalyukin
I think one extra migration fits well:
我认为一个额外的迁移很合适:
class AddIndexes < ActiveRecord::Migration
def self.up
add_index :prices, :user_id
add_index :prices, :price
end
def self.down
remove_index :prices, :user_id
remove_index :prices, :price
end
end
Or you can use changesyntax with newer versions of rails, look at DonamiteIsTntcomment for details:
或者您可以change在较新版本的 rails 中使用语法,查看DonamiteIsTnt注释以获取详细信息:
class AddIndexes < ActiveRecord::Migration
def change
add_index :prices, :user_id
add_index :prices, :price
end
end
回答by Erik Peterson
Once an app is in production, the intent is that migrations will be applied once.
应用程序投入生产后,目的是迁移将应用一次。
If you're still developing your app, you can always add them as you've noted followed by a rake db:migrate:resetthis will wipe your database and re-create it.
如果您仍在开发您的应用程序,您可以随时添加它们,正如您所指出的那样,然后添加一个rake db:migrate:reset这将擦除您的数据库并重新创建它。
Otherwise, create a new migration rails g migration add_user_id_index.
否则,创建一个新的迁移rails g migration add_user_id_index。
class AddUserIdIndex < ActiveRecord::Migration
def self.up
add_index :prices, :user_id
end
def self.down
remove_index :prices, :user_id
end
end
FWIW, add_index :pricesdoesn't make sense. Indexes are per-column, not per-table.
FWIW,add_index :prices没有意义。索引是每列,而不是每表。
You can always manually create indexes by logging into your database.
您始终可以通过登录数据库来手动创建索引。
CREATE INDEX prices__user_id__idx ON prices (user_id);
回答by Marcel Hymanwerth
Simple solution:
简单的解决方案:
- create a new migration
- add the indexes there (they don't need to be in the older migrations)
- run the migrations
- 创建一个新的迁移
- 在那里添加索引(它们不需要在旧的迁移中)
- 运行迁移

