Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Validation: allow_nil & allow_blank don't work correctly #441

Closed
Zhomart opened this Issue · 1 comment

2 participants

@Zhomart

As ActiveRecord's validation says, (http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html)

:allow_nil - Skip validation if attribute is nil.
:allow_blank - Skip validation if attribute is blank.

The code below doesn't work:

class User
  key :confirmation_token, String, :unique => true, :allow_nil => true, :allow_blank => true
end

# I have one user with confirmation_token = nil in DB
user = User.new
user.valid? # => false
user.errors # => @messages={:confirmation_token=>["Already exists!"]

So, it validates for uniqueness anyway

@cheald
Owner

I don't think that this is actually a bug - MongoDB doesn't allow multiple nil or blank fields in a unique index, whereas ActiveRecord's target DBs do.

> db.uniques.insert({name: null})
> db.uniques.ensureIndex({name: 1}, {unique: 1})
> db.uniques.insert({name: null})
E11000 duplicate key error index: test.uniques.$name_1  dup key: { : null }

Allowing AR-style behavior would lead to actual index conflicts when you have a unique index defined on the collection.

@cheald cheald closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.