Add 'alias_id' config option to disable top-level _id/id aliasing #3472
+58
−5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Adds a new
alias_idconnection config option (default:true) that allows disabling the automatic top-level_id↔idaliasing introduced in v5.0.alias_idisfalse, root-levelidis treated as a regular field and not converted to/from_idalias_idistrue(default), behavior is identical to current — zero breaking changesalias_id, also setrename_embedded_id_fieldtofalseto prevent compound query operators ($and/$or) from incorrectly aliasing root-levelidfields in nested where clausesProblem
Applications upgrading from v4 to v5 that have documents with a business
idfield (e.g., external API IDs, legacy system identifiers) alongside MongoDB's_idexperience data corruption because the aliasing overwrites the customidfield with_id.The existing
rename_embedded_id_fieldconfig (v5.3+) only controls embedded document aliasing. There is currently no way to disable top-level aliasing, which has been reported in:Changes
src/Connection.phpaliasIdproperty, getter/setter, config init ('alias_id' => true)src/Query/Builder.phpcompileWheres(): gateid→_idcolumn rename ongetAliasId()src/Query/Builder.phpaliasIdForQuery(): separate root aliasing (alias_id) from embedded aliasing (rename_embedded_id_field)src/Query/Builder.phpaliasIdForResult(): same separation for result processingsrc/Eloquent/DocumentModel.phpgetIdAttribute(): return actualidattribute without_idfallback when disabledsrc/Eloquent/DocumentModel.phpgetKeyName(): return_idas primary key when disabledsrc/Eloquent/DocumentModel.phpgetKey(): return raw_idattribute directly, bypassing accessorConfig usage
Or at runtime:
Why both config keys?
The
aliasIdForQuery()method recurses into nested arrays withroot=false. Compound query operators like$and/$or(automatically added by SoftDeletes or multiplewhere()clauses) create nested array structures that are incorrectly treated as embedded documents during recursion. Settingrename_embedded_id_fieldtofalseprevents this from converting root-levelidto_idinside these query structures.Test plan
alias_id => true)