Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


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

Zhomart opened this Issue · 1 comment

2 participants


As ActiveRecord's validation says, (

: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

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

So, it validates for uniqueness anyway


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.