Possible to specify unique index with NULLs allowed in Rails/ActiveRecord?

后端 未结 2 1754
失恋的感觉
失恋的感觉 2021-02-06 22:15

I want to specify a unique index on a column, but I also need to allow NULL values (multiple records can have NULL values). When testing with PostgreSQ

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-06 22:59

    Your migration will work and will allow multiple null values (for the most database engines).

    But your validation for the user class should look like below.

    validates :email, uniqueness: true, allow_nil: true
    

提交回复
热议问题