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/persistable/updatable.rb:199
# Checks to see if the _id field has been modified. If it has, and if
# the document has already been persisted, this is an error. Otherwise,
# returns without side-effects.
#
# Note that if `Mongoid::Config.immutable_ids` is false, this will do
# nothing.
#
# @raise [ Errors::ImmutableAttribute ] if _id has changed, and document
# has been persisted.
def enforce_immutability_of_id_field!
# special case here: we *do* allow the _id to be mutated if it was
# previously nil. This addresses an odd case exposed in
# has_one/proxy_spec.rb where `person.create_address` would
# (somehow?) create the address with a nil _id first, before then
# saving it *again* with the correct _id.
return unless _id_changed? && !_id_was.nil? && persisted?
raise Errors::ImmutableAttribute.new(:_id, _id) if Mongoid::Config.immutable_ids
Mongoid::Warnings.warn_mutable_ids
end
# Consolidates all the callback invocations into a single place, to
# avoid cluttering the logic in #prepare_update.
#
# @param [ Array<Document> ] update_children The children that the
# :update callbacks will be executed on.
def run_all_callbacks_for_update(update_children, &block)
run_callbacks(:commit, with_children: true, skip_if: -> { in_transaction? }) do
run_callbacks(:save, with_children: false) do
run_callbacks(:update, with_children: false) do
run_callbacks(:persist_parent, with_children: false) do
_mongoid_run_child_callbacks(:save) do
_mongoid_run_child_callbacks(:update, children: update_children, &block) # _mongoid_run_child_callbacks :update
end # _mongoid_run_child_callbacks :save
end # :persist_parentView on GitHub (pinned to 0da0e23d71)
When it happens
Trigger: Thrown at lib/mongoid/persistable/updatable.rb:199 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/3b7ea371c9d037b1.
Report an issue: GitHub.