Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable datetimes are used for timestamps marked as not null in PGX / GO #3837

Open
benjaco opened this issue Feb 12, 2025 · 1 comment
Open
Labels

Comments

@benjaco
Copy link

benjaco commented Feb 12, 2025

Version

1.28.0

What happened?

For pgx, pgtype.Timestamp is used for TIMESTAMP (and other time related types) regardless if the column is nullable or not.

time.Time was expected to be used for timestamps marked with NOT NULL

"database/sql" is handling it correctly https://play.sqlc.dev/p/e1e23e0d41d3d1340a4a009099bb81b589385b49187b89fd40c9aee4c33097cf

Database schema

CREATE TABLE authors (
  id   BIGSERIAL PRIMARY KEY,
  name text      NOT NULL,
  bio  text,
  
  born TIMESTAMP      NOT NULL,
  died  TIMESTAMP
);

SQL queries

-- name: ListAuthors :many
SELECT * FROM authors
ORDER BY name;

Configuration

{
  "version": "2",
  "sql": [{
    "schema": "schema.sql",
    "queries": "query.sql",
    "engine": "postgresql",
    "gen": {
      "go": {
        "out": "db",
        "sql_package": "pgx/v5"
      }
    }
  }]
}

Playground URL

https://play.sqlc.dev/p/7ac654a59b8d17aa0f7e9bd8dfd689bec9d035bc8e8ea7a5a1beb078be531769

What operating system are you using?

No response

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@benjaco benjaco added the bug Something isn't working label Feb 12, 2025
@benjaco benjaco changed the title Nullable datetimes are used for timestaps marked as not null in PGX / GO Nullable datetimes are used for timestamps marked as not null in PGX / GO Feb 12, 2025
@benjaco
Copy link
Author

benjaco commented Feb 12, 2025

I would guess that the not null checks here should be moved up as the first check

case "pg_catalog.timestamp", "timestamp":
if driver == opts.SQLDriverPGXV5 {
return "pgtype.Timestamp"
}
if notNull {
return "time.Time"
}
if emitPointersForNull {
return "*time.Time"
}
return "sql.NullTime"

As it is with the string types

case "text", "pg_catalog.varchar", "pg_catalog.bpchar", "string", "citext", "name":
if notNull {
return "string"
}
if emitPointersForNull {
return "*string"
}
if driver == opts.SQLDriverPGXV5 {
return "pgtype.Text"
}
return "sql.NullString"

Is there a reason that it issn't? If not, then I could go ahead and do the pr for this on all cases where the null check is below the pgx check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant