user974881

user974881

Pattern match postgex result - (MatchError) no match of right hand side value: %Postgrex.Result

I am trying to do pattern matching against a postgrex result, as such:

{:ok, %Postgrex.Result{rows: [[result]]}} = Postgrex.query!(:postgrex, "SELECT COUNT(1)
      FROM pages
      WHERE url = $1;", [url])

but I get

** (Mix) Could not start application dbhandler: DBHandler.Application.start(:normal, []) returned an error: shutdown: failed to start child: DBHandler
    ** (EXIT) an exception was raised:
        ** (MatchError) no match of right hand side value: %Postgrex.Result{command: :select, columns: ["count"], rows: [[1]], num_rows: 1, connection_id: 17022, messages: []}
            (dbhandler 0.1.0) lib/dbhandler.ex:139: DBHandler.private_in_db?/1
            (dbhandler 0.1.0) lib/dbhandler.ex:70: anonymous fn/1 in DBHandler.init/1
            (elixir 1.17.3) lib/enum.ex:987: Enum."-each/2-lists^foreach/1-0-"/2
            (dbhandler 0.1.0) lib/dbhandler.ex:69: DBHandler.init/1
            (stdlib 4.3.1.6) gen_server.erl:851: :gen_server.init_it/2
            (stdlib 4.3.1.6) gen_server.erl:814: :gen_server.init_it/6
            (stdlib 4.3.1.6) proc_lib.erl:240: :proc_lib.init_p_do_apply/3

What I might be doing wrong is that I didn’t put all the elements of the map, but I don’t THINK I have to do that.
So, is there a way to do this that works, or do I have to find a way other than pattern matching?

Marked As Solved

al2o3cr

al2o3cr

Postgrex.query! raises a Postgrex.Error exception if anything goes wrong, so it returns an Postgrex.Result without an {:ok, ...} wrapper.

You could either remove the {:ok, ...} part on the left-hand side or switch the right-hand side to Postgrex.query (no !), depending on exactly what you want to happen if the query fails.

Also Liked

Schultzer

Schultzer

I see you are fiddling with hand written queries, you might be interested in GitHub - elixir-dbvisor/sql: Brings an extensible SQL parser and sigil to Elixir, confidently write SQL with automatic parameterized queries. so that your queries are automatically parametrized.

D4no0

D4no0

Looks like an amazing alternative to ecto queries!

Schultzer

Schultzer

My goal is not to be an alternative, but a more fundamental building block for SQL based adapters https://groups.google.com/g/elixir-ecto/c/8MOkRFAdLZc

There is a potential for making Ecto.SQL more powerful and maintainable, by reusing code across adapters.

user974881

user974881

it does look quite interesting, I’ll try to use it, and I have currently a problem so this might fix it!

user974881

user974881

Well, I’m trying to do this:

query = ~SQL[from pages]
  |> ~SQL[where url not in #{state.lock}]
  |> ~SQL[where host not in #{state.politeness}]
  |> ~SQL"select url"

Where state.lock and state.politeness are lists of strings.
But it just waits and does nothing when I use to_sql on it…

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
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
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
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
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

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
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
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