Case-insensitive unique index in Rails/ActiveRecord?

前端 未结 8 1631
天命终不由人
天命终不由人 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 04:07

    I would suggest (just as an opportunity to consider among others) to use two separate fields:

    • email
    • email_original

    The first one is always down cased and thus can be uniquely indexed in a database agnostic manner, while the second field keeps verbatim to what user enetered and can have upper characters.

    Obviously in User model one then needs to set email based on email_original on each save and prohibit direct mangling with email field.

提交回复
热议问题