docs: clarify index shifting in cast_assoc/3 when using drop_param#4705
Merged
josevalim merged 3 commits intoelixir-ecto:masterfrom Feb 16, 2026
Merged
Conversation
josevalim
reviewed
Feb 15, 2026
lib/ecto/changeset.ex
Outdated
| The element originally at index `2` ("Third St") will be passed to the `:with` | ||
| function with position `1`, because the list is compacted after the drop | ||
| operation. If you need to preserve the original semantic positions, ensure | ||
| you also provide a `:sort_param`. |
Member
There was a problem hiding this comment.
Can we preserve the original positions even with sort_param? If an element is removed, no element can ever be at index 2, right?
Contributor
Author
There was a problem hiding this comment.
You're right, the positions are not preserved since the collection is compacted into a list. I'm removing that last line. Thanks for the feedback, I appreciate it! -> 1268a5a
Member
|
Thank you and congratulations @JuanmiAcosta on your first PR! ❤️ I have dropped one tiny question! |
josevalim
reviewed
Feb 16, 2026
Member
|
💚 💙 💜 💛 ❤️ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
While the behavior of
:drop_paramand:sort_paramis documented, this PR adds a specific clarification regarding the index shifting that occurs when elements are dropped but not explicitly re-sorted.Context:
When using a map for many-style associations, Ecto compacts the remaining elements into a sequential list after applying
:drop_param. As a result, the index passed to an arity-3:withfunction reflects the final position in the list, which might differ from the original map key.Contribution:
Explicitly mentions that remaining elements are re-indexed sequentially starting from 0.
Adds a visual example to illustrate the difference between the original parameter key and the final position received by the changeset function.
This addition aims to help developers avoid subtle bugs when using the :with option to persist order (e.g., in a :position column), making the underlying list transformation more explicit.
Note: This is my first contribution to Ecto (and to an open-source project). I've found this specific case while working on a production system and thought it would be a valuable addition to the documentation for other developers. Feedback is more than welcome!