ZhangZhihui's Blog  

Problem: sqlc can't recognize PostgreSQL data types numeric and decimal with the default database driver package database/sql, it makes them string.

 

Using the PostgreSQL driver pgx, Sqlc can make them pgtype.Numeric type, but still can't overrides them to float64:

version: "2"
sql:
  - engine: "postgresql"
    queries: "db/query"
    schema: "db/schema"
    gen:
      go:
        package: "db"
        out: "db/sqlc"
        sql_package: "pgx/v5"
        emit_json_tags: true
        emit_interface: true
        emit_empty_slices: true
        overrides:
          - db_type: "numeric"
            go_type: "float64"
          - db_type: "decimal"
            go_type: "float64"

 

Have to change gptye.Numeric to float after the codes are generated:

    # manually change pgtype.Numeric to float64
    sed -i 's/pgtype.Numeric/float64/g' db/sqlc/*.go
    goimports -w db/sqlc/*.go
    gofmt -w db/sqlc/*.go
    sed -i 's/\t/    /g' db/sqlc/*.go

 

posted on 2023-11-29 08:40  ZhangZhihuiAAA  阅读(93)  评论(0)    收藏  举报