英文:
SQLC Override Bool Type PostgreSQL
问题
我正在使用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
,这可能吗?
英文:
I am using SQLC and my YAML config file contains the following type overrides for 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"
Everything here works, but if I add:
- db_type: "bool"
nullable: true
go_type:
import: "gopkg.in/guregu/null.v4"
package: "null"
type: "Bool"
I do not get the expected result. I have also tried boolean
and bit
to no avail, both with and without nullable
.
I have an update query defined here:
-- 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 *;
But the generated struct looks like:
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"`
}
I want to use null.Bool
instead of sql.NullBool
, is this possible?
答案1
得分: 1
请稍等,我会为您翻译这段内容。
英文:
Create your schema.yaml
like this:
CREATE TABLE users (
...
active pg_catalog.bool
)
In sqlc.yaml
the entry should look something like this:
- db_type: "pg_catalog.bool"
nullable: true
go_type:
import: "gopkg.in/guregu/null.v4"
package: "null"
type: "Bool"
Then after sqlc generate
it would look like this:
type SetUserParams struct {
...
Active null.Bool `json:"active"`
}
So it uses null.Bool
instead of sql.NullBool
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论