fireproofsocks

fireproofsocks

Ecto delete a record WITHOUT selecting first

Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.html#c:delete/2 has examples, but they are all doing 2 operations: a select, then a delete.

In the interest of optimization, is it possible with Ecto to delete a record with a single operation? Something like the SQL query DELETE FROM my_table WHERE id=123 ?

Is it considered bad form to delete without selecting first?

Thanks for pointers, as always.

Marked As Solved

xlphs

xlphs

from(x in DataModel, where: x.id == ^id) |> Repo.delete_all

Also Liked

OvermindDL1

OvermindDL1

Can also look at repo.delete_all/2, it accepts a query for doing just that. :slight_smile:

Adzz

Adzz

Ypu can do something like this:

%MyStruct{id: 10} |> MyApp.Repo.delete

That is just build a struct with the id in it then delete

fireproofsocks

fireproofsocks

I tried that, but that generates an error:
** (Ecto.StaleEntryError) attempted to delete a stale struct

fireproofsocks

fireproofsocks

Thank you! Just to show a more thorough example:

{rows_deleted, _} = from(x in DataModel, where: x.id == ^id) |>; Repo.delete_all

You might get zero as the number of rows deleted.

Huolong

Huolong

I think you have to do a query to pass existing rows to delete or delete_all,
because they require the Ecto metadata element of the struct to contain the ‘loaded’ flag.
The only alternative would be to completely fake the whole struct,
including all the internal fields and flags.

But I agree with you that there should be a version of delete,
which just takes a primary key value, by analogy with the ‘get’ function.

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
New
makeitrein
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
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
bsollish-terakeet
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

We're in Beta

About us Mission Statement