Supporting nullable embeds_many entries?

15 views
Skip to first unread message

Austin Ziegler

unread,
Apr 4, 2024, 3:07:12 PMApr 4
to elixi...@googlegroups.com
I opened a discussion last night on Elixir Forum about clearing an `embeds_many` field, and I’m somewhat dissatisfied with the result and wondering what it would take to support allowing `cast_embed(:field)` where the parameter is `nil` on an `embeds_many` and then setting to the content of the column to `NULL`.


I’d like to have a check constraint on the database that is something like `options IS NULL OR (jsonb_type(options) == 'array' AND jsonb_array_length(options) > 1)`, because for my data type, an empty list is explicitly illegal. The absence of value in this case is `NULL`, not `[]`.

I’m less concerned about the loaded form (where the default is set to `[]` from `nil`), but it feels wrong, too.

I did a small bit of experimentation adding a `nullable` option, and figured out a lot of the levers to pull to make something like this work, but ultimately got to a case where `change/4` was called and since I *can* work around this for now via JSON/GraphQL serialization, stopped the investigation.

If there are other suggestions on how I might achieve what I am after in Elixir (I could probably also do this with a BEFORE INSERT or BEFORE UPDATE database trigger), that would be great. Otherwise, if this is an idea that could be supported (because embeds aren’t *really* relations for SQL databases; that is not necessarily the case for document databases), with some pointers I would be able to look at tackling this after my current project.

-a
--

José Valim

unread,
Apr 4, 2024, 4:21:13 PMApr 4
to elixi...@googlegroups.com
I am almost sure this has been discussed and the reason is to keep it in sync with associations. They don’t/cant differ from null and empty list. 

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/CAJ4ekQsCdfzjn4xH1jXJW76x3-X3sJL1PLhveyrMCzvs33yaBA%40mail.gmail.com.

Austin Ziegler

unread,
Apr 4, 2024, 6:07:08 PMApr 4
to elixi...@googlegroups.com
On Apr 4, 2024, at 16:21, José Valim <jose....@dashbit.co> wrote:


I am almost sure this has been discussed and the reason is to keep it in sync with associations. They don’t/cant differ from null and empty list. 

I think they should be able to do so. It may be an unusual case, but the thread linked in the discussion points out at least one case where this would be the case different than my case, related to allow lists:
  • Null means no allow list so default rules or all cases allowed
  • [] means an empty allow list so no cases allowed
  • [_|_] means only the items present are allowed
Associations are fundamentally different than embeds in that embeds are fields in relations whereas associations are relations.

I’m not sure how this should be handled, but I think that some sort of approach needs to be explored.

-a

Austin Ziegler

unread,
Apr 5, 2024, 12:01:35 PMApr 5
to elixi...@googlegroups.com
I have worked around this by defining a custom `Ecto.Type` (https://elixirforum.com/t/clearing-an-embeds-many-entry-cast-embed-3/62698/9, but I still think that `embeds_many` should not behave like a relation (unless "required: true"), at least with SQL databases. I don't and won't use MongoDB, so I don’t particularly care what happens with it.

-a
Reply all
Reply to author
Forward
0 new messages