tanweerdev

tanweerdev

Prepare_query wont pass updated options to preloaded sub sequent calls from main query

I am trying to implement soft delete using this library ecto_soft_delete/soft_delete_repo.ex at master · revelrylabs/ecto_soft_delete · GitHub 's module This has prepare_query method implemented which will not fetch all soft deleted records which is fine. But the problem is, it will also not fetch preloaded records if they are soft deleted. eg if I am a user with salary of type monthly. if monthly salary type got soft deleted. it will not show/fetch any salary type at all… Main query shouldn’t fetch soft deleted records which is fine but all preloaded records despite of soft deleted or not should be fetched…
so I tried to modify this query a bit

def prepare_query(_operation, query, opts) do
        schema_module = get_schema_module_from_query(query)
        fields = if schema_module, do: schema_module.__schema__(:fields), else: []
        soft_deactivateable? = Enum.member?(fields, :deactivated_at)

        if has_include_deactivated_at_clause?(query) || opts[:with_deactivated] ||
             !soft_deactivateable? do
          {query, opts}
        else
          query = from(x in query, where: is_nil(x.deactivated_at))
          IO.inspect opts
          # I am trying put with_deactivated for all sub-sequent calls but sub-sequent calls dont receive this option
          opts = Keyword.put(opts, :with_deactivated, false)
          {query, opts}
        end
      end

prepare_query will get called two times. one for the main user query and one for salary_type… but second query(which is for preload) should always fetch record means with with_deactivated: false but i am unable to differentiate which query is the main one and which query is for preload in prepare_query

Most Liked

al2o3cr

al2o3cr

That project hasn’t seem much change lately, and there are 2-year-old somewhat-contradictory issues that seem related:

What you’re looking for sounds like it would be better achieved by explicitly filtering on deleted_at in the main query and preloading normally otherwise; approaches like prepare_query are hard to opt-out of.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
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
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
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 Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
sergio
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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
ycv005
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
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement