Skip to content

Commit b161d34

Browse files
authored
MONGOID-5228 disallow _id to be updated on persisted documents (#5542)
* MONGOID-5228 - disallow _id to be updated on persisted documents Previously, updating _id on any document (top-level or embedded) would silently ignore the _id attribute, making it appear as though the operation succeeded. This was misleading, since the _id didn't actually change. This change makes it so that _id is explicitly made immutable once a document has been persisted. The same behavior is intentionally enforced regardless of whether the document is embedded or not. If you find yourself needing to update the _id field in an embedded document, you'll need to use the lower-level driver methods to accomplish this. We believe modifying the _id field (even in embedded documents) is an anti-pattern. * use a specialized error for immutability violations * add a feature flag * change the flag name, and account for existing nested attribute behavior * wrong default for <9.0 * clean up a test that was unnecessarily sensitive to the _id changing * another test that was unnecessarily sensitive to _id changes * more tests that are too sensitive to _id changes * we need to allow _id mutations if the _id was previously nil * mention enforcement of _id immutability in the release notes
1 parent 6687c07 commit b161d34

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

source/release-notes/mongoid-9.0.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,25 @@ This means that, by default, Mongoid 9 will update the existing document and
204204
will not replace it.
205205

206206

207+
The immutability of the ``_id`` field is now enforced
208+
-----------------------------------------------------
209+
210+
Prior to Mongoid 9.0, mutating the ``_id`` field behaved inconsistently
211+
depending on whether the document was top-level or embedded, and depending on
212+
how the update was performed. As of 9.0, changing the ``_id`` field will now
213+
raise an exception when the document is saved, if the document had been
214+
previously persisted.
215+
216+
Mongoid 9.0 also introduces a new feature flag, ``immutable_ids``, which
217+
defaults to ``true``.
218+
219+
.. code-block:: ruby
220+
221+
Mongoid::Config.immutable_ids = true
222+
223+
When set to false, the older, inconsistent behavior is restored.
224+
225+
207226
Bug Fixes and Improvements
208227
--------------------------
209228

0 commit comments

Comments
 (0)