sqlite3 on rails: create_table using collate nocase

限于喜欢 提交于 2019-12-02 03:20:47

Doesn't look like collation on an index has been merged yet:

https://github.com/rails/rails/pull/18499/files

This PR, adds collation support for columns by the way.

The syntax looks like:

create_table :foo do |t|
  t.string :string_nocase, collation: 'NOCASE'
  t.text :text_rtrim, collation: 'RTRIM'
end

add_column :foo, :title, :string, collation: 'RTRIM'

change_column :foo, :title, :string, collation: 'NOCASE'

For people searching, if 'collate' is still not available in your version of rails (you'll get "Unknown key: :COLLATE"), then you have to create the index manually:

execute("CREATE INDEX 'index_events_on_name' ON 'events' ('name' COLLATE NOCASE);")

Then to search using the index, you can do:

Event.where("name collate nocase == ?",name)
Event.where("name LIKE ?",name)

The 'find_by_name' won't be case insensitive. I believe you can do this by putting the 'collate nocase' on the create table instead of in the index (this will also need to use an "execute()" call. This means that you will always be forced to match case-insensitive, i.e., this will also be case-insensitive:

Event.where("name == ?",name)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!