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   ZhangZhihuiAAA  阅读(57)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
 
点击右上角即可分享
微信分享提示