jjabba
Correctly deal with Ecto.Schema.Metadata for cascading delete
Im working with a database where table B has a foreign key (not null) referencing table A. A and B has a one-to-one relationship.
Furthermore the fk in Table B is created with the delete_all option
references(:A, on_delete: :delete_all)
most importantly, all deletions of b are done by deleting the corresponding a entry. Relying on the on the cascading delete functionality of the database.
Q:
Is there a nice way to implement a helper
def delete_b(b) do
# preload a,
# delete a
# return {:ok, b} with b.__meta__: #Ecto.Schema.Metadata<:deleted, "b">,
end
so that the b returned has the proper metadata state?
(I’ve resorted to a hack for now…
defp delete_b(b) do
b = Repo.preload(b, :a)
with {:ok, _deleted_a} <- Repo.delete(b.a) do
meta = Kernel.struct(%Ecto.Schema.Metadata{}, %{state: :deleted, source: "b"})
deleted_b = Kernel.struct(%B{}, %{id: b.id, __meta__: meta}))
{:ok, deleted_b}
else
err -> err
end
end
)
Popular in Questions
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”:
14:57:30.512 [warn] ...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project.
Baby step #1 is extracting the number ...
New
I have a list say
x = ["23gh", "56kh", "97mh"]
I would like to pass each element to Val in each iteration.
Say, in iteration 1 -------...
New
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work.
Or rather, not char, but a substr...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Other popular topics
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir? ...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New







