【问题标题】:How to create a "collate nocase" column in a table in migration?如何在迁移中的表中创建“collat​​e nocase”列?
【发布时间】:2012-06-27 12:00:07
【问题描述】:

这个问题的答案https://stackoverflow.com/a/973785/1297371How to set Sqlite3 to be case insensitive when string comparing? 告诉如何使用“COLLATE NOCASE”列创建表。

我的问题是如何在 Rails 迁移中创建这样的列? 即如何从

create table Test
(
  Text_Value  text collate nocase
);

create index Test_Text_Value_Index
  on Test (Text_Value collate nocase);

做:

create_table :tests do
  #WHAT HERE?
end
add_index #... WHAT HERE?

【问题讨论】:

    标签: ruby-on-rails sqlite migration collation


    【解决方案1】:

    我添加了新的迁移,我删除并重新创建了索引,例如:

    class AddIndexToWordVariations < ActiveRecord::Migration
    
      def change
    
        remove_index :word_variations, :variation_word
    
        add_index :word_variations, :variation_word, :COLLATE => :NOCASE
    
      end
    
    end
    

    'word_variations' 是我的桌子

    【讨论】:

    【解决方案2】:

    对于搜索的人,如果您的 rails 版本中仍然没有“collat​​e”(您将获得“Unknown key: :COLLATE”),那么您必须手动创建索引:

    execute("create index Test_Text_Value_Index on Test (Text_Value collate nocase);")
    

    在您的“def up”中(“drop_table”也会在您的“def down”中删除索引)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-10
      • 2013-02-03
      • 2014-02-03
      • 2016-05-15
      • 2016-06-09
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      相关资源
      最近更新 更多