siddhant3030

siddhant3030

How can I write a raw sql query?

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 queries in phoenix. So to understand

I have created a user schema that inserts an email in a database table.

So created the changeset the like this

def changeset(user, attrs) do
user
|> cast(attrs, [:email, :password])
end

Now I have a create function that I’m using in my model. So the model will basically talk to db.

def create_user(attrs \\ %{}) do
    %User{}
    |> User.changeset(attrs)
    |> Repo.insert()
end

I have inserted an email in my database. using this
iex(1) > user = User.changeset(%User{}, %{email: “”, password: “”})

Now what I want is to find that user by his email address.
Also If I wanted to fetch through id. I would just do this

def get_user!(id), do: Repo.get!(User, id)

But what if I want to write a raw query? How do I do that?

Marked As Solved

sribe

sribe

Ah! Seeing your result, I maybe see your problem. You’re expected the $1 inside the quotes to get replaced with the argument? Maybe what you actually want is:

Ecto.Adapters.SQL.query(Wizex.Repo, "SELECT * FROM game WHERE name ILIKE $1", [name <> "%"])

Also Liked

kokolegorille

kokolegorille

You might also have a look at…

…for raw queries

al2o3cr

al2o3cr

FWIW, you can do this with Repo.get_by(User, email: email_to_find).

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

The best place to start is always the docs. Hint: it’s a function inside this module Ecto.Repo — Ecto v3.14.0

Last Post!

sribe

sribe

As Luca pointed out, it will search for a name that begins with the literal string “$1#”

Where Next?

Popular in Questions Top

New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 131117 1222
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement