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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews