英文:
sqlc.yaml config isn't going to override postgresql interval to time.Duration
问题
我发现了一个与 sqlc codegen app 相关的问题。在某些情况下,当我需要一个 interval
(postgresql)字段时,sqlc 生成了一个带有 int64
字段的对象。这个解决方案看起来有问题,并且在扫描行时会创建一个错误:Errorf("cannot convert %v to Interval", value)
。
sqlc.yaml:
version: "2"
overrides:
go:
overrides:
- db_type: "interval"
engine: "postgresql"
go_type:
import: "time"
package: "time"
type: "https://pkg.go.dev/time#Duration"
sql:
- queries: "./sql_queries/raffle.query.sql"
schema: "./migrations/001-init.sql"
engine: "postgresql"
gen:
go:
package: "raffle_repo"
out: "../repo/sql/raffle_repo"
sql_package: "pgx/v4"
schema.sql:
create table windowrange
(
id serial primary key,
open timestamp not null ,
duration interval not null,
created_at timestamp default now(),
updated_at timestamp default now(),
raffle_id integer not null
constraint raffle_id
references raffle
on delete cascade
);
生成的模型:
type Windowrange struct {
ID int32
Open time.Time
Duration int64
CreatedAt sql.NullTime
UpdatedAt sql.NullTime
RaffleID int32
}
通过将该字段的类型更改为 time.Duration
,问题很快得到了解决,代码也开始正常工作。但是这段代码是由代码生成器生成的,看起来是一个不好的决定。
在尝试通过 sqlc.yaml 配置文件覆盖类型时,我没有得到任何结果,对象仍然是 int64 类型。我在哪里出错了,如何修复这个问题?
英文:
I've found a problem with an sqlc codegen app. In case, when I need an interval
(postgresql) field, sqlc generates an object with int64
field. This solution looks broken and creates an error while scanning a row: Errorf("cannot convert %v to Interval", value)
sqlc.yaml:
version: "2"
overrides:
go:
overrides:
- db_type: "interval"
engine: "postgresql"
go_type:
import: "time"
package: "time"
type: "https://pkg.go.dev/time#Duration"
sql:
- queries: "./sql_queries/raffle.query.sql"
schema: "./migrations/001-init.sql"
engine: "postgresql"
gen:
go:
package: "raffle_repo"
out: "../repo/sql/raffle_repo"
sql_package: "pgx/v4"
schema.sql:
create table windowrange
(
id serial primary key,
open timestamp not null ,
duration interval not null,
created_at timestamp default now(),
updated_at timestamp default now(),
raffle_id integer not null
constraint raffle_id
references raffle
on delete cascade
);
generated model:
type Windowrange struct {
ID int32
Open time.Time
Duration int64
CreatedAt sql.NullTime
UpdatedAt sql.NullTime
RaffleID int32
}
It was quickly fixed by making this field of type time.Duration and code becomes working, but this code were codegened and it looks like bad decision.
While trying to override types by an sqlc.yaml config I have nothig, object is still creating of int64 type. Where I'm wrong and how can I fix this?
答案1
得分: 0
在支持的类型中,你会看到pg_catalog.interval
也是Postgres中支持的interval
值之一。
因此,如果你只想使用time.Duration
而不是int64
,你需要将你的overrides
部分更改为:
overrides:
go:
overrides:
- db_type: "pg_catalog.interval"
engine: "postgresql"
go_type:
import: "time"
type: "Duration"
提示:如果对于最明显的数据类型不起作用,你可以尝试另一种类型。
英文:
In the supported types, you will see that pg_catalog.interval
is also one of the supported values for interval
in Postgres.
So, if you just want to use time.Duration
instead of int64
, you need to change your overrides
section to:
overrides:
go:
overrides:
- db_type: "pg_catalog.interval"
engine: "postgresql"
go_type:
import: "time"
type: "Duration"
Tip: if it doesn't work for the most obvious data type, you can try another one.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论