在我的Rails应用程序中,使用PostgreSQL数据库,我有一个表,看起来像这样:
create_table "person_identifiers", force: :cascade do |t|
t.string "identifier", limit: 255
end我想在该列上添加一个索引。我通过以下迁移来实现这一点:
execute('create index person_identifiers_identifier on person_identifiers using gin (identifier gin_trgm_ops)')这是按照预期工作的,所以在模式中我有以下索引:
add_index "person_identifiers", ["identifier"], name: "person_identifiers_identifier", using: :gin但是我想让这个索引不区分大小写,所以我写道:
execute('create index person_identifiers_identifier on person_identifiers using gin (lower(identifier) gin_trgm_ops)')但不幸的是,这在schema.rb中是不可见的?我知道我可以使用SQL格式而不是schema.rb,但我想坚持在schema.rb中看到这一点。
发布于 2015-08-14 01:00:54
已经询问并回答了similar question。但是,PostgreSQL提供了一种不区分大小写的文本数据类型,称为citext。我在许多应用程序中使用过这种数据类型,其中字符串的大小写并不重要,例如用户的电子邮件地址。这样,即使字符串的大小写不同,也可以匹配该列上的查询。它还会影响唯一性约束,因此两个字符序列相同、大小写不同的字符串现在被视为相同。
要在Rails中使用citext,您将需要activerecord-postgresql-citext gem (这需要Rails版本4)。一旦安装了gem,就可以使用以下命令将列迁移到citext
change_column :person_identifiers, :identifier, :citext
请记住,这是不区分大小写的文本列,而不是字符串列。
https://stackoverflow.com/questions/31914031
复制相似问题