Case-insensitive unique index in Rails/ActiveRecord?

前端 未结 8 1611
天命终不由人
天命终不由人 2020-12-09 03:38

I need to create a case-insensitive index on a column in rails. I did this via SQL:

execute(
   \"CREATE UNIQUE INDEX index_users_on_lower_email_index 
    O         


        
8条回答
  •  伪装坚强ぢ
    2020-12-09 03:52

    I think you need column name(s) as below

       add_index "users", [email], {:name => "index_users_on_lower_email_index", :unique => true }
    

    And you have to make email field in database with proper Case Insensitive collation, that way your index will be also case insensitive.

    depending of db engine which you are using it syntax may be different but

    alter table [users] alter column [email] varchar(250) collate utf8_general_ci ...
    

    and when you add index to this column it will be case insensitive.

提交回复
热议问题