mongodb/mongoid · error · Mongoid::Errors::ImmutableAttribute

Attempted to change the immutable attribute '%{name}' with t

Error message

Attempted to change the immutable attribute '%{name}' with the value: %{value}.

What it means

Error "Attempted to change the immutable attribute '%{name}' with the value: %{value}." thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/association/nested/one.rb:165

        # immutable) is being asked to change. If so, raise an exception.
        #
        # If Mongoid::Config.immutable_ids is false, this will do nothing,
        # and the update operation will fail silently.
        #
        # @raise [ Errors::ImmutableAttribute ] if _id has changed, and
        #   the document has been persisted.
        def check_for_id_violation!
          # look for the basic criteria of an update (see #update?)
          return unless existing&.persisted? && !destroyable?

          # if the id is either absent, or if it equals the existing record's
          # id, there is no immutability violation.
          id = extracted_id
          return if existing._id == id || id.nil?

          # otherwise, an attempt has been made to set the _id of an existing,
          # persisted document.
          raise Errors::ImmutableAttribute.new(:_id, id) if Mongoid::Config.immutable_ids

          Mongoid::Warnings.warn_mutable_ids
        end
      end
    end
  end
end

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/association/nested/one.rb:165 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mongodb/mongoid@0da0e23d71 (2026-08-23). Data as JSON: /api/errors/b63ad1a2e4496d9c. Report an issue: GitHub.