PostgreSQL 中 SQLC 覆盖布尔类型
时间:2024-02-28
浏览:338
作者:佚名
IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《PostgreSQL 中 SQLC 覆盖布尔类型》,聊聊,我们一起来看看吧!
问题内容
我正在使用 sqlc,并且我的 yaml 配置文件包含 postgresql 的以下类型覆盖:
gen:
go:
emit_json_tags: true
package: "hmdb"
out: "hmdb"
overrides:
- db_type: "hstore"
nullable: true
go_type: "github.com/jackc/pgtype.hstore"
- db_type: "text"
nullable: true
go_type:
import: "gopkg.in/guregu/null.v4"
package: "null"
type: "string"
- db_type: "timestamptz"
nullable: true
go_type:
import: "gopkg.in/guregu/null.v4"
package: "null"
type: "time"
这里的一切都有效,但如果我添加:
- db_type: "bool"
nullable: true
go_type:
import: "gopkg.in/guregu/null.v4"
package: "null"
type: "bool"
我没有得到预期的结果。我也尝试过 boolean 和 bit 无论有没有 nullable 都无济于事。
我在这里定义了一个更新查询:
-- name: setuser :one
update users set
username = coalesce(sqlc.narg(username), username),
email = coalesce(sqlc.narg('email'), email),
phone = coalesce(sqlc.narg('phone'), phone),
password = coalesce(sqlc.narg('password'), password),
mfatoken = coalesce(sqlc.narg('mfatoken'), mfatoken),
active = coalesce(sqlc.narg('active'), active)
where id = $1 returning *;
但生成的结构如下所示:
type SetUserParams struct {
ID uuid.UUID `json:"id"`
Username null.String `json:"username"`
Email null.String `json:"email"`
Phone null.String `json:"phone"`
Password null.String `json:"password"`
MFAToken null.String `json:"mfatoken"`
Active sql.NullBool `json:"active"`
}
我想使用 null.bool 而不是 sql.nullbool,这可能吗?
正确答案
像这样创建 schema.yaml:
create table users (
...
active pg_catalog.bool
)
在 sqlc.yaml 中,条目应如下所示:
- db_type: "pg_catalog.bool"
nullable: true
go_type:
import: "gopkg.in/guregu/null.v4"
package: "null"
type: "bool"
然后在 sqlcgenerate 之后它看起来像这样:
type SetUserParams struct {
...
Active null.Bool `json:"active"`
}
因此它使用 null.bool 而不是 sql.nullbool。
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.29
11.29
11.29
11.29
11.29
11.29
热门专题
