Skip to content

wrong pog.Date format causes crash #54

Description

@binajmen

Description

When using pog to insert data into PostgreSQL nullable columns, passing an empty string ("") causes a runtime error. This is a common issue with PostgreSQL drivers since PostgreSQL treats empty strings differently from NULL values. Currently, pog crashes with an Erlang error instead of handling this case gracefully.

Current Behavior

When attempting to insert an empty string into a nullable PostgreSQL column:

The empty string is passed directly to PostgreSQL
pog crashes with an Erlang error:

runtime error: Erlang error
No Erlang function clause matched the arguments it was called with.
stacktrace:
  pog_ffi.convert_error
  pog_ffi.query
  pog.execute

Expected Behavior

There are two potential solutions to consider:

  • Handle empty strings gracefully: Convert empty strings to NULL values automatically when inserting into nullable columns
  • Better error handling: Provide a clear error message when an empty string is provided for a nullable column, explaining that NULL should be used instead

Steps to Reproduce

Create a PostgreSQL table with a nullable column:

create table if not exists transactions (
    id serial primary key,
    account varchar(50) not null,
    counterparty_account varchar(50)  -- nullable
);

Create an insert query:

insert into transactions (
    account,
    counterparty_account
) values (
    $1,  -- account (varchar(50))
    $2   -- counterparty_account (varchar(50), nullable)
)
returning id

Attempt to insert a record with an empty string for the nullable field:

sql.insert_transaction(
  pog,
  tx.account,
  ""  // empty string for counterparty_account
)

For a quick reproduction, you can clone this repo:

https://github.com/binajmen/budget

Additional Context

Technical Details

pog version: 3.2.0
PostgreSQL version: 17
Gleam version: 1.6.2

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