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
--