Shouldn't on_replace: :update delete the relation when nil supplied with cast_assoc on has_one relations?

120 views
Skip to first unread message

Ivan Yurov

unread,
Jul 8, 2020, 11:18:29 AM7/8/20
to elixir-ecto
I'm using cast_assoc with has_one relation, `on_replace` is set to :update.
It correctly updates fields of the relation, but when I cast nil, it basically just nilifies the foreign_key on the side of the relation, while I think the correct behavior should be to mark the changeset for deletion.
Any good reason it doesn't work this way? Or how this behavior could be implemented?

José Valim

unread,
Jul 8, 2020, 11:20:46 AM7/8/20
to elixi...@googlegroups.com
If it is set to nil, the behaviour should be controlled by on_delete.

--
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/7e4d10c7-1706-43cc-90cf-db2e97fca0f2o%40googlegroups.com.

Ivan Yurov

unread,
Jul 8, 2020, 11:31:11 AM7/8/20
to elixi...@googlegroups.com
on_delete on which relation? In my case it's `post - has_one - poll`. So if I set `on_delete: :delete_all` there, it means that if I delete post, poll also has to be deleted, and it brings no effect to my use case. On the side of a poll I cannot even use this option, cause it's a `belongs_to`. 

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/4BjBoQBQ7XM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/CAGnRm4JhWO8obk2VqOjCFLBmZq%3DCR5-zOGuiB10LgCmzumENoA%40mail.gmail.com.


--
Kind regards,
Ivan Yurov

José Valim

unread,
Jul 8, 2020, 11:43:44 AM7/8/20
to elixi...@googlegroups.com
Oh, I see. Yes, we would need to add a new type of :on_replace. Perhaps on_replace: {:update, :delete} (and the current one would translate to on_replace: {:update, :nillify}. PRs are welcome.

Ivan Yurov

unread,
Jul 8, 2020, 11:53:52 AM7/8/20
to elixi...@googlegroups.com
Sure, will do that. But is that really the best default behavior for update that it nilifies foreign key? For example, if we make :update to delete on nil, then we just need one extra type :update_and_nilify, which I believe should be much less common. 

On Wed, Jul 8, 2020 at 5:43 PM José Valim <jose....@dashbit.co> wrote:
Oh, I see. Yes, we would need to add a new type of :on_replace. Perhaps on_replace: {:update, :delete} (and the current one would translate to on_replace: {:update, :nillify}. PRs are welcome.

--
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/4BjBoQBQ7XM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-ecto...@googlegroups.com.

José Valim

unread,
Jul 8, 2020, 5:31:38 PM7/8/20
to elixi...@googlegroups.com
Regardless if it is or isn’t, changing it would be backwards incompatible.

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/CAAoaZy6nHPU1Eg-uN6h_fqnX0a8XhBKcNGb%2BAcFQfoDMGhv4cw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages