sqlc-dev / sqlc

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

Can't override 'bigint' to be uint32 #3184

Open MisterSweety opened 9 months ago

MisterSweety commented 9 months ago

Version

Other

What happened?

I am writing a service which can receive uint32 values that it has to persist in a postgres database. I am using sqlc to generate the go code to interact with the database. My database design uses 'bigint' for these id columns. By default the generated code expects the type int64 for the bigint id fields but I want to override this to be uint32 as that is more inline with reality. However, adjusting my sqlc.yaml file to include this override does not have the desired effect as the generated code still expects int64

Relevant log output

No response

Database schema

No response

SQL queries

No response

Configuration

version: '2'
sql:
  - schema: './../../../../cmd/postgres/migrations'
    queries: './queries'
    engine: 'postgresql'
    strict_order_by: false
    gen:
      go:
        package: 'generated'
        out: 'PGgenerated'
        emit_json_tags: true
        json_tags_case_style: 'snake'
        overrides:
          - db_type: pg_catalog.bigint
            go_type: uint32


### Playground URL

_No response_

### What operating system are you using?

macOS

### What database engines are you using?

PostgreSQL

### What type of code are you generating?

Go
jakoguta commented 9 months ago

@EasyEesteren I believe implementing such an override may be risky. Others may choose to use uint64 and this may cause out of range errors in PostgreSQL as it does not natively support unsigned integers at the moment.

alarbada commented 9 months ago

@EasyEesteren I believe implementing such an override may be risky. Others may choose to use uint64 and this may cause out of range errors in PostgreSQL as it does not natively support unsigned integers at the moment.

It might be risky, but what sqlc 1.25 is doing now is really confusing. If the override is not going to be applied a warning would be very helpful at the very least.

I have a similar problem as @EasyEesteren with this configuration:

version: "2"
sql:
  - schema: "server/db/schema.sql"
    queries: "server/db/queries.sql"
    engine: "postgresql"
    gen:
      go:
        package: "db"
        out: "server/db"
        overrides:
          - db_type: "bigint"
            go_type: "int"

This is generating bigints as int64 in my models.go, but technically speaking this should be valid right?

defany commented 8 months ago

Is there any progress?

I have the simillar issue with this types:

 - db_type: pg_catalog.numeric
   go_type: int

But Im getting this:

type AddChatMessageParams struct {
    ChatID    *int64
    UserID    pgtype.Numeric
    Text      string
    Timestamp pgtype.Timestamp
}
kyleconroy commented 8 months ago

Sorry that folks are running into issues with overrides. They sadly can be tricky to use (they weren't designed super well). I cant' help you debug these issues without seeing the database schema and associated query, as that determines which types the overrides see.

Can you reproduce on the playground and post the results here?

a1eks3y commented 3 days ago

I have same problem (1.27.0). Override working for bigserial, but not for bigint ` version: "2" sql: