What is being marked as invalid for belongs_to?

defmodule Test.Posts.Post
    belongs_to :user, User, on_replace: :mark_as_invalid

defmodule Test.Users.User
   has_many :posts, Post

I tried reading the documentation and did not really understand what would happen here. Can someone explain exactly what this means here? does it mean if I delete the user then all the posts would be marked as invalid and essentially be orphaned? I tried locally and it seemed to delete all the posts which is why I am a bit confused.

Taken from the docs

:mark_as_invalid - if attempting to remove the association or embedded data via parent changeset - an error will be added to the parent changeset, and it will be marked as invalid

What it means is that if you try to delete the post by via updating the User, it will put an error in your user update changeset.

1 Like