Skip to content

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

Description

@jaras

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);

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions