user974881

user974881

Hello, I am trying to do a postgrex query with the sql library where I get the first entry from a table sorted by last_visited and excluding all entries where url is in state.lock or host is in state.politeness.

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

    {:ok, %Postgrex.Result{rows: [[response]]}} = Postgrex.query(:postgrex, query, params)

However, when I run this I get:

** (MatchError) no match of right hand side value: {:error, %Postgrex.Error{message: nil, postgres: %{code: :syntax_error, file: "scan.l", line: "1244", message: "syntax error at or near \"$0\"", pg_code: "42601", position: "40", routine: "scanner_yyerror", severity: "ERROR", unknown: "ERROR"}, connection_id: 7202, query: "select url from pages where url not in $0 where host not in $0"}}

Does anyone know how to fix this?

Showing Posts 1 to 10

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Can you print the SQL it generates ?

christhekeele

christhekeele

It looks like the query it generates is hidden in the provided error message:

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Whoops!

Right so @user974881 you can’t use SQL in with parameters like that. Instead do:

not host = any?(#{state.lock})
ruslandoga

ruslandoga

:waving_hand:

Double where might also be a problem.

Schultzer

Schultzer

Hey there, as I said in the other thread Pattern match postgex result - (MatchError) no match of right hand side value: %Postgrex.Result - #14 by Schultzer this I would need a bit more time to fix, it’s not on you.

I haven’t impl. double where in the sense that it would be accumulating ands. And I would say it’s an open question of how that should work.

I appreciate you’re taking the library for a ride, it gives me a clear picture of what your expectations are and where I need to improve.

Although I’m currently prioritizing a rewrite of the parser, since the first iteration has been handwritten and does not even parse DDL.

user974881

user974881 OP

Well, I tried this:

{query, params} = ~SQL[from pages]
      |> ~SQL[where not url = any?(#{state.lock}) and not host = any?(#{state.politeness})]
      |> ~SQL"select url"
      |> to_sql()

    {:ok, %Postgrex.Result{rows: [[response]]}} = Postgrex.query(:postgrex, query, params)

so that I only have one where and also for your solution, and now I get:

** (MatchError) no match of right hand side value: {:error, %Postgrex.Error{message: nil, postgres: %{code: :undefined_parameter, file: "parse_param.c", line: "140", message: "there is no parameter $0", pg_code: "42P02", position: "44", routine: "variable_paramref_hook", severity: "ERROR", unknown: "ERROR"}, connection_id: 4546, query: "select url from pages where not url = any ($0) and not host = any ($0)"}}
Schultzer

Schultzer

If you are on main you might be able to get away with this: sql/test/string_test.exs at 35d743c7715da9336269dfd7ea8426f4d2fa7ec6 · elixir-dbvisor/sql · GitHub

But your example might be an edge case that I haven’t gotten to yet.

user974881

user974881 OP

now I get

** (MatchError) no match of right hand side value: {:error, %Postgrex.Error{message: nil, postgres: %{code: :syntax_error, file: "scan.l", line: "1244", message: "syntax error at or near \"$0\"", pg_code: "42601", position: "40", routine: "scanner_yyerror", severity: "ERROR", unknown: "ERROR"}, connection_id: 5621, query: "select url from pages where url not in $0 and host not in $1"}}

with

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

    {:ok, %Postgrex.Result{rows: [[response]]}} = Postgrex.query(:postgrex, query, params)
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You added a space that shouldn’t be there after any.

Taking a step back, are you sure this is the right library for you? The sql library seems aimed at people familiar with SQL and who prefer to use that syntax instead of the ecto query syntax. It doesn’t seem like this is syntax you’re super comfortable with though and the process of using ecto would be a lot simpler for what you’re doing.

user974881

user974881 OP

I’m not using any, I’m using in(which then is converted to any by sql), and in the code I used before I do not see the extra space.

And I do think it is better I use sql as the problem is more with elixir than with SQL.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews