Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-2153

Need an :upsert validation context?

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.0
    • Affects Version/s: None
    • Component/s: None

      Do we need another validation context in valid?, i.e. :upsert?.

      class Thing
      include Mongoid::Document

      field :email, type: String
      field :name, type: String

      validates :email, uniqueness: true
      end

      t1 = Thing.new(email: 'me@me.com', name: 'Me'); t1.id = 'custom-id'; t1.upsert
      t2 = Thing.new(email: 'me@me.com', name: 'You'); t2.id = 'custom-id'; t2.upsert # validation fails even though the upsert would otherwise update the first model and keep the db valid

      Suggest we need to be able to:

      class Model
      validates :foo, uniqueness: true, on: [:create, :update] # but not on :upsert
      end

      Admittedly it's not ideal as it would allow upsert to be used to put the db in an invalid state:

      t3 = Thing.new(email: 'me@me.com', name: 'Me'); t3.id = 'a-different-custom-id'; t3.upsert # gets inserted, breaking the uniqueness constraint

      , but it's better than removing the validation entirely I think.

            Assignee:
            Unassigned Unassigned
            Reporter:
            al Antonio Lagrotteria
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: