ryanwinchester

ryanwinchester

In Ecto.Changeset — Ecto v3.14.0 there is a part which says:

The :delete option in particular must be used carefully as it would allow users to delete any associated data. If you need deletion, it is often preferred to add a separate boolean virtual field to the changeset function that will allow you to manually mark it for deletion, as in the example below: […]

defmodule Comment do
  use Ecto.Schema
  import Ecto.Changeset

  schema "comments" do
    field :body, :string
    field :delete, :boolean, virtual: true
  end

  def changeset(comment, params) do
    cast(comment, params, [:body, :delete])
    |> maybe_mark_for_deletion
  end

  defp maybe_mark_for_deletion(changeset) do
    if get_change(changeset, :delete) do
      %{changeset | action: :delete}
    else
      changeset
    end
  end
end

However, this example is incomplete and I am having trouble applying that.

Carrying on with the comment theme, If I have this as the schema that inserts it with cast_assoc, where can I mark it as deleted? :

(I know it’s weird to have one comment but that’s the type of relationship I’m using in my actual code, so I want to keep the example similar)

defmodule Post do
  use Ecto.Schema
  import Ecto.Changeset

  schema "posts" do
    field(:title, :string)
    field(:body, :string)
    has_one(:comment, Comment)
  end

  def update_changeset(post, attrs) do
    post
    |> Repo.preload(:comment)
    |> cast(attrs, [:title, :body])
    |> cast_assoc(:comment, required: true, on_replace: :nilify)
  end
end

Showing Posts 1 to 3

nico_amsterdam

nico_amsterdam

The complete explanation is here

so in schema “comments” you have correctly:
field :delete, :boolean, virtual: true
and if you update a Post containing a Comment and this comment has the value ‘true’ for the ‘delete’ field, it will be marked for deletion, and it will be deleted.

I did find the name ‘on_replace’ confusing. It specifies what to do when you don’t send an update for a child that does exists in the database. I would call it something like: ‘on_missing’ or ‘on_no_update_for’.
And the only values it can have are :raise (default), :mark_as_invalid, :nilify, :update, or :delete.
Maybe it’s me, but what I want is an option like :ignore / :nothing.

ryanwinchester

ryanwinchester OP

But won’t that mark the latest comment for deletion?

I want the one being replaced to be marked for deletion…

nico_amsterdam

nico_amsterdam

In your case a post can have only one comment, so there is only one comment that could be deleted.
So either you update the Post with the Comment with delete=true in the comment, and that comment (with the same id) will be delete,
or you specify on_replace: :delete and update the Post without any comments or with a new comment, and the on_replace will delete the existing comment.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews