{"record":{"id":"3b7ea371c9d037b1","repo":"mongodb/mongoid","slug":"attempted-to-change-the-immutable-attribute-nam-3b7ea3","errorCode":null,"errorMessage":"Attempted to change the immutable attribute '%{name}' with the value: %{value}.","messagePattern":"Attempted to change the immutable attribute '%(.+?)' with the value: %(.+?)\\.","errorType":"exception","errorClass":"Mongoid::Errors::ImmutableAttribute","httpStatus":null,"severity":"error","filePath":"lib/mongoid/persistable/updatable.rb","lineNumber":199,"sourceCode":"\n      # Checks to see if the _id field has been modified. If it has, and if\n      # the document has already been persisted, this is an error. Otherwise,\n      # returns without side-effects.\n      #\n      # Note that if `Mongoid::Config.immutable_ids` is false, this will do\n      # nothing.\n      #\n      # @raise [ Errors::ImmutableAttribute ] if _id has changed, and document\n      #   has been persisted.\n      def enforce_immutability_of_id_field!\n        # special case here: we *do* allow the _id to be mutated if it was\n        # previously nil. This addresses an odd case exposed in\n        # has_one/proxy_spec.rb where `person.create_address` would\n        # (somehow?) create the address with a nil _id first, before then\n        # saving it *again* with the correct _id.\n\n        return unless _id_changed? && !_id_was.nil? && persisted?\n        raise Errors::ImmutableAttribute.new(:_id, _id) if Mongoid::Config.immutable_ids\n\n        Mongoid::Warnings.warn_mutable_ids\n      end\n\n      # Consolidates all the callback invocations into a single place, to\n      # avoid cluttering the logic in #prepare_update.\n      #\n      # @param [ Array<Document> ] update_children The children that the\n      #   :update callbacks will be executed on.\n      def run_all_callbacks_for_update(update_children, &block)\n        run_callbacks(:commit, with_children: true, skip_if: -> { in_transaction? }) do\n          run_callbacks(:save, with_children: false) do\n            run_callbacks(:update, with_children: false) do\n              run_callbacks(:persist_parent, with_children: false) do\n                _mongoid_run_child_callbacks(:save) do\n                  _mongoid_run_child_callbacks(:update, children: update_children, &block) # _mongoid_run_child_callbacks :update\n                end # _mongoid_run_child_callbacks :save\n              end # :persist_parent","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/persistable/updatable.rb#L181-L217","documentation":"Error \"Attempted to change the immutable attribute '%{name}' with the value: %{value}.\" thrown in mongodb/mongoid.","triggerScenarios":"Thrown at lib/mongoid/persistable/updatable.rb:199 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"0da0e23d71adfbad31128c18e5e2c8c301c6f7c4","analyzedAt":"2026-08-23T06:25:47.656Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}