[Proposal] Allow for opting-in to raising if trying to violate a field's :writable configuration

30 views
Skip to first unread message

David Green

unread,
Jun 12, 2026, 10:50:44 PM (2 days ago) Jun 12
to elixir-ecto
The :writable option for schema fields is extremely helpful for read-only fields. However, the current behavior is to silently ignore updates to the field. While this does ultimately prevent the field from being updated in the database, it can lead to code which appears to be making changes to a field when it is not doing so in reality. It would be great if there were an option to opt-in to raising if attempting to insert a record with writable: :never or update a record with writable: :insert to detect attempts to errantly update these fields.

I would also be happy to make a docs PR specifying the current behavior, as the docs currently just state "cannot be further modified", without insight as to how that is actually accomplished.

Thanks!

José Valim

unread,
Jun 13, 2026, 4:08:32 AM (yesterday) Jun 13
to elixi...@googlegroups.com
That's a good point. Thoughts on raising vs warning?


--
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 visit https://groups.google.com/d/msgid/elixir-ecto/42ddb761-02ca-42eb-8280-b785990be8d2n%40googlegroups.com.

David Green

unread,
Jun 13, 2026, 10:29:28 AM (yesterday) Jun 13
to elixi...@googlegroups.com
Hmm, good question! I hadn't really considered warning. 

For my usecases I think I typically would want to raise to ensure, for example, that tests loudly fail if such a violation is detected. Similarly, at runtime if I am unexpectedly violating the annotation, I probably have a bug that is potentially getting the system into an unexpected state which I would like to raise so the operation is aborted and it gets loudly reported (i.e. to Sentry) for me to fix.

However, I can definitely see people wanting just a warning so it doesn't actively crash things in prod while still being detectable.

Perhaps something like:

field :foo, :string, writable: [when: :never, on_violation: :warn]

Where :when could have values :always, :never, and :insert (like today) and :on_violation could have values :nothing, :warn, and :raise.

Such an approach would also allow Ecto to continue to accept writable: :never and internally translate it to writable: [when: :never, on_violation: :nothing] (or on_violation: :warn if we wanted to change the default behavior to warn instead) in a backwards compatible manner.

WDYT?

You received this message because you are subscribed to a topic in the Google Groups "elixir-ecto" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-ecto/kloQ5uJ-Xss/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-ecto...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/elixir-ecto/CAGnRm4J449kcU4ywSr1wgMJwjP8uHJEHP2My5GaEGH2F44n-4A%40mail.gmail.com.

José Valim

unread,
Jun 13, 2026, 11:17:28 AM (yesterday) Jun 13
to elixi...@googlegroups.com
Another option would be `writable: [:insert, :warn]`. Unsure. Can you try a PR with a list for us to see how the code will look like? Thanks!


David Green

unread,
Jun 13, 2026, 11:30:12 AM (yesterday) Jun 13
to elixi...@googlegroups.com
Absolutely, thanks for the feedback Jose!

Reply all
Reply to author
Forward
0 new messages