silverdr
I have a Parent model, which embeds_many() children models. Children are then stored in a jsonb column as a List (Array in JSON) of simple, two-key maps (Objects), where values of one of the keys of those maps need to be unique. Something like:
[
{"name":"name0", "amount":"10.00"},
{"name":"name1", "amount":"12.20"},
{"name":"name2", "amount":"10.00"},
[…]
{"name":"nameN", "amount":"56.00"}
]
Value of name has to be unique. I handled the uniqueness on create/update and this works. But then I continuously spend cycles all around the application converting the retrieved list into a single map like:
%{
"name0" => "10.00",
"name1" => "12.20",
"name2" => "10.00",
[…]
"nameN" => "56.00"
}
Any suggestions for a “proper” way to get the data stored as a single map (JSON object), while retaining the possibility to use Phoenix form helpers / inputs_for, validations and other goodies?
Custom Ecto type? Or? Some examples, maybe?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #ai
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
This is full of caveats:
inputs_foronly works for related assocs or embeds, which are one or a list of related records. There’s nothing like a map for related records.:mapor{:map, value_type}native types of ecto are not supported by phoenix form handling natively. You can make things work by manually creating input names and assigning values to inputs, but it’s not covered ootb. Also using an ecto type over a relationship means you forego a bunch of their features like e.g. per item errors, sort/drop support, likely a few more…So really I’d treat the data like you do and let the goal of it becoming a map be an implementation detail. You could consider separating the “write model” from the “read model” here and let the read model read the data from the db in the correct format / into a
:mapfield.silverdr
Thank you, Benjamin. I spent good part of the day trying to figure something reasonable out and I ran into basically all the caveats you listed with no apparent solution. At least none that would not make things even more smelly than they are now.
One thing made me curious though. Since I have the write part working, and the main problem is with the fact that I read the data in the form it is stored (i. e. as List of Maps rather than a Map), then – maybe – the separation of models you mentioned is worth checking.
How does one do this? Any examples or links to documentation?
LostKobrakai
Something like this:
Could even consider
Ecto.ParameterizedTypeif you don’t like hardcoding key/value keys.MarcusRiemer
I’m in a similar situation right now and did something similar as suggested here: I expect every module that is used as the value type to define a
newfunction and call that in order toloadthe data which currently means to callMyModule.changesetandEcto.Changeset.apply_changes. The data will always be transformed from a JSON list representation:In this usage example
Entitywould need to define anew/1function which converts the given map to a struct. And the returned struct must have field callednamewhich will serve as the key when transforming from List to Map.The actual implementation of
EctoSupport.EmbeddedMaplooks like this:This however turns out to not work recursively: If the
Entitydefines another field usingEmbeddedMap, that nested field is stored as the Map which breaks upon deserialization. I suppose I need to properly use thedumperprovided to properly dig into the nested data, but so far I haven’t figured out how that would need to be called or passed. Could anyone here give me a pointer? The documentation at Ecto.ParameterizedType — Ecto v3.14.0 eluded me.MarcusRiemer
I got this to work recursively, but I think I have a more precise question now: Is there some public Ecto API to
dumpa whole struct?When writing testcases for nested datasets I figured out that Ecto probably uses something along the lines of
Ecto.Type.dump(MyModule.__schema__(:type, :field))to dump individual fields. So I’m now doing the same when dumping map values. The new implementation looks like this:This gives me recursion on my own types and has so far passed my synthetic and practical tests. But if feels weird as I …
Access.key!hoops to do the manipulation on the struct on the fly …EmbeddedMap.So there is probably a nicer way to do this.
MarcusRiemer
Additional warning for anyone following this route: If you miss the
def embed_as(_), do: :dumpthat was given in the example, you are going to have a painful time debugging why your maps are “sometimes” stored directly as map instead of being converted to a list … In my case this only surfaced for types that are children ofpolymorphic_embeds_manyorpolymorphic_embeds_one.