decapo01

decapo01

Ecto named querys in raw sql statements

Is it possible to do named querys with ecto like this

query = "INSERT INTO widgets (id, name) VALUES (:id, :name)"

Ecto.Adapters.SQL.query(query, %{ :id => 1, :name => "Jim" })

Most Liked

yurko

yurko

The query function takes string query as argument, but according to the documentation you can pass a string with numbered params and then these params:

Ecto.Adapters.SQL.query(MyRepo, "SELECT $1::integer + $2", [40, 2])`

so you can save your query as a string variable and pass it around like that.

OvermindDL1

OvermindDL1

As you can see at:
https://github.com/elixir-ecto/ecto/blob/master/lib/ecto/adapters/sql.ex#L224
The query call requires the Repo (so it knows what syntax to use for the specific database and all such and to call it), the string, and the arguments (as a list), and you are passing in just a string and arguments. :slight_smile:

However yes, but instead of using things like :id you need to use the index in the list that it is pulling from (plus 1) as well as you should type it so the SQL knows what type it should be (this may be optional in most cases though), so your example should be:

query = "INSERT INTO widgets (id, name) VALUES ($1, $2)"

Ecto.Adapters.SQL.query(MyWhateverRepo, query, [1, "Jim"])

It is all documented with examples at the docs: https://hexdocs.pm/ecto/Ecto.Adapters.SQL.html#query/4

It would be quite possible to have your example work, but you’ll need to write a helper (and maybe PR it?) as that is not how it is currently specified. :wink:

Where Next?

Popular in Questions 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
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

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
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
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
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
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement