sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
13.21k stars 803 forks source link

Type name resolution differs in `database` mode (vs. traditional / static analysis) #2912

Open dhermes opened 1 year ago

dhermes commented 1 year ago

Version

1.23.0

What happened?

PostgreSQL column types resolve to different names in "database" mode vs. "static analysis" mode (i.e. the "old way"). Two examples encountered in our production codebase (a long-time user of sqlc):

Database Static analysis
html5_email_address app.html5_email_address
numeric pg_catalog.numeric

In both cases the schema is absent in "database" mode.

Relevant log output

No response

Database schema

CREATE SCHEMA s;

CREATE DOMAIN s.html5_email_address AS TEXT;

CREATE TABLE s.person (
  id         UUID PRIMARY KEY,
  first_name TEXT NOT NULL,
  last_name  TEXT NOT NULL,
  email      s.html5_email_address NOT NULL,
  height     NUMERIC NOT NULL
);

SQL queries

-- name: GetPerson :one
SELECT
  p.id,
  p.email,
  p.height
FROM
  s.person AS p
WHERE
  id = @id;

Configuration

No response

Playground URL

https://play.sqlc.dev/p/bf27993cf03969b4f19f4f1d955220188538bb833a447489e2c9825c798f4cbe

What operating system are you using?

Linux, macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

dhermes commented 1 year ago

I want to be clear, we can workaround it by just covering both types of resolved column type name

overrides:
  go:
    overrides:
      - go_type: string
        db_type: app.html5_email_address
        nullable: false
      - go_type: database/sql.NullString
        db_type: app.html5_email_address
        nullable: true
      # --------------------------------------------
      - go_type: string
        db_type: html5_email_address
        nullable: false
      - go_type: database/sql.NullString
        db_type: html5_email_address
        nullable: true
      # --------------------------------------------
      - go_type: github.com/shopspring/decimal.Decimal
        db_type: pg_catalog.numeric
        nullable: false
      - go_type: github.com/shopspring/decimal.NullDecimal
        db_type: pg_catalog.numeric
        nullable: true
      # --------------------------------------------
      - go_type: github.com/shopspring/decimal.Decimal
        db_type: numeric
        nullable: false
      - go_type: github.com/shopspring/decimal.NullDecimal
        db_type: numeric
        nullable: true

I'm just filing the issue to let you know that there is a "drift" in the actually resolved name and it may represent a larger issue.