Postgrex error with sql library: message: "syntax error at or near \"$0\""

My bad, I’ve not documented the change in interpolation, instead of #{} then you should use double braces {{}} like how it’s done in heex. I only mention it here: Add generated lexer and parser by Schultzer · Pull Request #5 · elixir-dbvisor/sql · GitHub

with

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

IO.inspect({query, params})

I get

iex(1)> DBHandler.page_to_scrape()

20:07:41.399 [error] GenServer DBHandler terminating
** (UndefinedFunctionError) function nil.token_to_string/1 is undefined
    nil.token_to_string({:select, [line: 0, column: 6, file: nil], [{:ident, [line: 0, column: 10, file: nil], [~c"url"]}]})
    (elixir 1.18.3) lib/enum.ex:1714: Enum."-map/2-lists^map/1-1-"/2
    (sql 0.1.0) lib/sql.ex:113: String.Chars.SQL.to_string/1
    (sql 0.1.0) lib/sql.ex:41: SQL.to_sql/1
    (dbhandler 0.1.0) lib/dbhandler.ex:136: DBHandler.handle_call/3
    (stdlib 4.3.1.6) gen_server.erl:1149: :gen_server.try_handle_call/4
    (stdlib 4.3.1.6) gen_server.erl:1178: :gen_server.handle_msg/6
    (stdlib 4.3.1.6) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
Last message (from #PID<0.573.0>): :page_to_scrape
State: %{lock: [], politeness: []}
Client #PID<0.573.0> is alive

    (stdlib 4.3.1.6) gen.erl:256: :gen.do_call/4
    (elixir 1.18.3) lib/gen_server.ex:1125: GenServer.call/3
    (elixir 1.18.3) src/elixir.erl:386: :elixir.eval_external_handler/3
    (stdlib 4.3.1.6) erl_eval.erl:748: :erl_eval.do_apply/7
    (elixir 1.18.3) src/elixir.erl:364: :elixir.eval_forms/4
    (elixir 1.18.3) lib/module/parallel_checker.ex:120: Module.ParallelChecker.verify/1
    (iex 1.18.3) lib/iex/evaluator.ex:336: IEx.Evaluator.eval_and_inspect/3
    (iex 1.18.3) lib/iex/evaluator.ex:310: IEx.Evaluator.eval_and_inspect_parsed/3
Database handler started!
** (exit) exited in: GenServer.call(DBHandler, :page_to_scrape, 60000)
    ** (EXIT) an exception was raised:
        ** (UndefinedFunctionError) function nil.token_to_string/1 is undefined
            nil.token_to_string({:select, [line: 0, column: 6, file: nil], [{:ident, [line: 0, column: 10, file: nil], [~c"url"]}]})
            (elixir 1.18.3) lib/enum.ex:1714: Enum."-map/2-lists^map/1-1-"/2
            (sql 0.1.0) lib/sql.ex:113: String.Chars.SQL.to_string/1
            (sql 0.1.0) lib/sql.ex:41: SQL.to_sql/1
            (dbhandler 0.1.0) lib/dbhandler.ex:136: DBHandler.handle_call/3
            (stdlib 4.3.1.6) gen_server.erl:1149: :gen_server.try_handle_call/4
            (stdlib 4.3.1.6) gen_server.erl:1178: :gen_server.handle_msg/6
            (stdlib 4.3.1.6) proc_lib.erl:240: :proc_lib.init_p_do_apply/3
    (elixir 1.18.3) lib/gen_server.ex:1128: GenServer.call/3
    iex:1: (file)

Are you on main, I just tried your query.

iex(9)> ~SQL[from pages] |>  ~SQL[where not url = any({{state.lock}}) and not host = any({{state.politeness}})] |> ~SQL"select url limit 1" |> SQL.to_sql
{"select url from pages where not url = any(?) and not host = any(?) limit 1",
 [true, false]}
defmodule Test do
    use SQL, adapter: SQL.Adapters.Postgres
    def query(state \\ %{lock: true, politeness: false}), do: ~SQL[from pages] |> ~SQL[where not url = any({{state.lock}}) and not host = any({{state.politeness}})] |> ~SQL"select url limit 1" |> SQL.to_sql
end
iex(1)> Test.query
{"select url from pages where not url = any($1) and not host = any($2) limit 1",
 [true, false]}

Well, it’s a list, not a boolean…

Yeah, I see no issues:

iex(2)> Test.query(%{lock: [1, 2, 3], politeness: [2, 3, 4]})
{"select url from pages where not url = any($1) and not host = any($2) limit 1",
 [[1, 2, 3], [2, 3, 4]]}
iex(3)> Test.query(%{lock: [1, 2, 3], politeness: ["2", "3", "4"]})
{"select url from pages where not url = any($1) and not host = any($2) limit 1",
 [[1, 2, 3], ["2", "3", "4"]]}

The issue might be that you need to pull from main there is an example with plug here: Mix — Mix v1.18.3

That I have, but now it’s not a problem, the function isn’t needed anymore.