mudspot

mudspot

Load data with Raw SQL statement

Hi,

Given an AshResource, how do I load data using raw sql statements?

PS. Use case: We have an existing sql load script that has complicated filter conditions what we don’t want to spend time rewriting in Ash Query

Best regards
Terry

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

The way that you would implement this would be with a manual action:

read :your_legacy_thing do
  manual fn _ash_query, ecto_query, context -> 
    # my suggestion, in order to get the most bang for your buck, is to use Ecto
    # here, and *join* to the ecto_query we provide.
  end
end

If you’re familiar with Ecto, this should be doable. You can also use YourRepo.query! to run raw SQL, but you will have two major drawbacks of this:

  1. you will lose whatever logic Ash already did to honor the original request
  2. you will need to post-process the results into records (not that hard, typically just `Enum.map(…, struct!(Resource, &1))

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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...
3271 130286 1222
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
vertexbuffer
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement