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

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
freewebwithme
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

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
nsuchy
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
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

We're in Beta

About us Mission Statement