sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Can't override 'bigint' to be uint32

Open EasyEesteren opened this issue 1 year ago • 2 comments

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

EasyEesteren avatar Feb 06 '24 16:02 EasyEesteren

@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.

jakoguta avatar Feb 11 '24 05:02 jakoguta

@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?

alarbada avatar Feb 11 '24 12:02 alarbada

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
}

defany avatar Feb 24 '24 13:02 defany

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?

kyleconroy avatar Mar 04 '24 02:03 kyleconroy