-
-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Identify type by a field of the parent schema #100
Comments
When loading from the db, we get just the map (in the Ecto Type) (we don't have the other fields of the schema), so we can't infer the type from any other fields outside the map. |
But when loading we have access to the |
You're right. I got confused. So let's talk about dumping into db. We don't have the other field values from the schema. |
Is it necessary? After casting we know the structure to be embedded so when dumping we could get the type for that structure. As far as I can see the proposed feature would impact only the casting process, the rest would remain as is. |
I guess you're right again..? 😂 But then we have 2 sources of truths for the type (in another adjacent field + in the actual embed). A developer could change the |
Never mind what I said, we might want to search on a text field rather than a jsonb field, so I see how that could be advantageous. As for the naming, How about But possible problem, what if in the changeset the developer changes the |
I started using the library yesterday and planned the schema of the table to have a distinct enum field in the table. And it turned out that I have to copy the field to the embedded data. For the time being, I decided not to add a type field, but it would be great to have an option to have such a design. |
I came to issues to check whether this was discussed to understand the rationale of being type field a part of the embedded data 🙂 |
Maybe imposing a priority in these methods can solve it, if the adjacent field is set, it is the source of truth otherwise we follow the The way I see the adjacent field, when set, will become tightly coupled to the embedded type, their values must not contradict each other, so when changing the value of I made it work with this function, right before the defp set_details_type(%{params: %{"details" => %{} = details}} = changeset, payment) do
method = get_change(changeset, :method) || payment.method
details
|> Map.keys()
|> List.first()
|> is_binary()
|> if(do: "__type__", else: :__type__)
|> then(&Map.put_new(details, &1, method))
|> then(&Map.put(changeset, :params, Map.put(changeset.params, "details", &1)))
end
defp set_details_type(changeset, _payment), do: changeset |
Yeah exactly! Sorry, rushed on replying the other comment. Kind answers the last question, as their values should not contradict each other, the change must occur using the changeset to be able to validate the right polymorphic type based on the adjacent field. |
About the name, maybe it could have a more common word than adjacent, how about |
Right, depends how you look at it. The field |
I think this might be completed via |
There is the option
identify_by_fields
used to infer the embedded schema type by the attributes it receives to cast. I'm thinking in a similar feature but looking at the value of a field of the parent schema. Something like:Or
Sounds a good feature or too specific?
The text was updated successfully, but these errors were encountered: