如何在 Ruby on Rails 迁移中使列唯一并为其编制索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1449459/
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
How do I make a column unique and index it in a Ruby on Rails migration?
提问by Tam
I would like to make a column uniquein Ruby on Rails migration script. What is the best way to do it? Also is there a way to index a column in a table?
我想unique在 Ruby on Rails 迁移脚本中创建一个专栏。最好的方法是什么?还有一种方法可以索引表中的列吗?
I would like to enforce uniquecolumns in a database as opposed to just using :validate_uniqueness_of.
我想强制执行unique数据库中的列,而不是仅使用:validate_uniqueness_of.
回答by ndp
The short answer:
简短的回答:
add_index :table_name, :column_name, unique: true
To index multiple columns together, you pass an array of column names instead of a single column name,
要将多个列一起索引,请传递列名数组而不是单个列名,
add_index :table_name, [:column_name_a, :column_name_b], unique: true
For finer grained control, there's a "execute" method that executes straight SQL.
对于更细粒度的控制,有一个“ execute”方法可以直接执行 SQL。
That's it!
就是这样!
If you are doing this as a replacement for regular old model validations, just check to see how it works. I'm not sure the error reporting to the user will be as nice. You can always do both.
如果您这样做是为了替代常规的旧模型验证,只需检查一下它是如何工作的。我不确定向用户报告的错误是否会很好。你总是可以同时做到这两点。
回答by d.danailov
rails generate migration add_index_to_table_name column_name:uniq
rails 生成迁移 add_index_to_table_name column_name:uniq
or
或者
rails generate migration add_column_name_to_table_name column_name:string:uniq:index
rails 生成迁移 add_column_name_to_table_name column_name:string:uniq:index
generates
产生
class AddIndexToModerators < ActiveRecord::Migration
def change
add_column :moderators, :username, :string
add_index :moderators, :username, unique: true
end
end
If you're adding an index to an existing column, remove or comment the add_columnline, or put in a check
如果您要向现有列添加索引,请删除或注释该add_column行,或进行检查
add_column :moderators, :username, :string unless column_exists? :moderators, :username
回答by Steve Grossi
Since this hasn't been mentioned yet but answers the question I had when I found this page, you can also specify that an index should be unique when adding it via t.referencesor t.belongs_to:
由于尚未提及这一点,但回答了我在找到此页面时遇到的问题,因此您还可以在通过t.referencesor添加索引时指定索引应该是唯一的t.belongs_to:
create_table :accounts do |t|
t.references :user, index: { unique: true } # or t.belongs_to
# other columns...
end
(as of at least Rails 4.2.7)
(至少 Rails 4.2.7)
回答by Pioz
If you are creating a new table, you can use the inline shortcut:
如果要创建新表,可以使用内联快捷方式:
def change
create_table :posts do |t|
t.string :title, null: false, index: { unique: true }
t.timestamps
end
end
回答by Nicholas Nelson
I'm using Rails 5 and the above answers work great; here's another way that also worked for me (the table name is :peopleand the column name is :email_address)
我正在使用 Rails 5,上面的答案很好用;这是另一种对我有用的方法(表名是:people,列名是:email_address)
class AddIndexToEmailAddress < ActiveRecord::Migration[5.0]
def change
change_table :people do |t|
t.index :email_address, unique: true
end
end
end
回答by Swaps
You might want to add name for the unique key as many times the default unique_key name by rails can be too long for which the DB can throw the error.
您可能希望为唯一键添加名称,因为 rails 的默认 unique_key 名称可能太长,数据库可能会抛出错误。
To add name for your index just use the name:option.
The migration query might look something like this -
要为您的索引添加名称,只需使用该name:选项。迁移查询可能看起来像这样 -
add_index :table_name, [:column_name_a, :column_name_b, ... :column_name_n], unique: true, name: 'my_custom_index_name'
add_index :table_name, [:column_name_a, :column_name_b, ... :column_name_n], unique: true, name: 'my_custom_index_name'
More info - http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_index
更多信息 - http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_index
回答by murali
add_index :table_name, :column_name, unique: true
To index multiple columns together, you pass an array of column names instead of a single column name.
要将多个列一起索引,请传递列名数组而不是单个列名。
回答by Ravistm
If you have missed to add unique to DB column, just add this validation in model to check if the field is unique:
如果您没有为 DB 列添加唯一值,只需在模型中添加此验证以检查该字段是否唯一:
class Person < ActiveRecord::Base
validates_uniqueness_of :user_name
end
refer hereAbove is for testing purpose only, please add indexby changing DB column as suggested by @Nate
请参阅此处以上仅用于测试目的,请按照@Nate 的建议通过更改 DB 列来添加索引
please refer this with indexfor more information
请参考此索引以获取更多信息

