lpil / pgo

🐘 Use PostgreSQL databases with PGO
https://hexdocs.pm/gleam_pgo/
Apache License 2.0
136 stars 12 forks source link

"exception error: no function clause matching" when query arguments not matching with execute args #29

Open jaras opened 4 months ago

jaras commented 4 months ago

When I run the query in the program attached below I get an exception that seems to originate from Erlang.

gleam run
  Compiling psqltest
   Compiled in 0.18s
    Running psqltest.main
exception error: no function clause matching 
                 gleam_pgo_ffi:convert_error(badarg) (/Users/jaras/code/psqltest/build/dev/erlang/gleam_pgo/_gleam_artefacts/gleam_pgo_ffi.erl, line 71)
  in function  gleam_pgo_ffi:query/3 (/Users/jaras/code/psqltest/build/dev/erlang/gleam_pgo/_gleam_artefacts/gleam_pgo_ffi.erl, line 68)
  in call from gleam@pgo:execute/4 (/Users/jaras/code/psqltest/build/dev/erlang/gleam_pgo/_gleam_artefacts/gleam@pgo.erl, line 193)
  in call from psqltest:main/0 (/Users/jaras/code/psqltest/build/dev/erlang/psqltest/_gleam_artefacts/psqltest.erl, line 25)

the issue I found was that token and expires was swapped in the sql statement, when those were swapped back it worked as expected.

import gleam/bit_array
import gleam/dynamic
import gleam/io
import gleam/pgo

pub fn main() {
  let db =
    pgo.connect(
      pgo.Config(
        ..pgo.default_config(),
        host: "localhost",
        database: "gleam_test",
        pool_size: 10,
      ),
    )

  let sql =
    "INSERT INTO sessions (user_id, token, expires) VALUES ($1, $2, $3) RETURNING id"

  let result =
    pgo.execute(
      sql,
      db,
      [
        pgo.int(3),
        pgo.timestamp(#(#(1, 2, 3), #(4, 5, 6))),
        pgo.bytea(bit_array.from_string("token")),
      ],
      dynamic.element(0, dynamic.int),
    )

  io.debug(result)
}
-- auto-generated definition
CREATE TABLE sessions
(
    id      bigserial
        PRIMARY KEY,
    token   bytea                                  NOT NULL
        UNIQUE,
    user_id integer                                NOT NULL,
    expires timestamp WITH TIME ZONE DEFAULT NOW() NOT NULL
);

ALTER TABLE sessions
    OWNER TO dkjanras;

CREATE INDEX sessions_token_index
    ON sessions (token);
lpil commented 3 months ago

Thanks for the report! I've been digging into this and I think it's a bug with PGO. I've opened an issue for it.