Commit 5d53d6f
committed
Scope the primary-key -> _id rename to the outermost document
The MongoDB convention of renaming the schema's primary-key field to
`_id` was being applied recursively. Once `Conversions.from_ecto_pk/2`
descended into a nested map (for example an `embeds_one` sub-document),
the parent schema's `pk` value was still threaded through, so any
nested field named `:id` was also rewritten to `:_id`.
That meant an embedded value like
%{jurisdiction: %{id: "MD", title: "Maryland"}}
was stored as
{jurisdiction: {_id: "MD", title: "Maryland"}}
even when the embedded schema declared its own `@primary_key {:id, :string,
autogenerate: false}`. Queries that filter on `jurisdiction.id` (the
common shape for apps that put a string identifier on a sub-document)
return nothing, and the wire format diverges from what other clients
(Ruby driver, Node driver, etc.) write for the same model.
The pk -> _id rename is only meaningful at the top of a document. Below
that, the field names are user-controlled and should be passed through
unchanged. This patch makes the recursive call inside `document/2` and
`document/3` pass `nil` for the pk argument, so the rename only fires
for the outermost document.
`Conversions.to_ecto_pk/2` (the read path) is left as-is for now. Its
rename of nested `"_id"` -> `Atom.to_string(pk)` only matters when a
sub-document actually contains an `_id` field, which is uncommon in
hand-written documents (Mongo only auto-adds `_id` to top-level docs).
A symmetric read-side change can follow separately if there's appetite.1 parent 6e114c0 commit 5d53d6f
1 file changed
Lines changed: 14 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
78 | 88 | | |
79 | 89 | | |
80 | 90 | | |
81 | 91 | | |
82 | 92 | | |
83 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
84 | 96 | | |
85 | 97 | | |
86 | 98 | | |
| |||
0 commit comments