首頁  >  文章  >  後端開發  >  sqlc.yaml 配置不會覆寫 postgresql 時間間隔到 time.Duration

sqlc.yaml 配置不會覆寫 postgresql 時間間隔到 time.Duration

WBOY
WBOY轉載
2024-02-09 20:09:181184瀏覽

sqlc.yaml 配置不会覆盖 postgresql 时间间隔到 time.Duration

php小編柚子在這裡為大家介紹一個關於sqlc.yaml配置的問題。使用postgresql時,我們常常需要使用時間間隔(time.Duration)來表示一段時間。然而,有時候我們會發現在sqlc.yaml設定檔中進行的時間間隔配置並不會生效,而是被預設的配置所覆蓋。這個問題該如何解決呢?請繼續閱讀本文,我們將給予詳細的解答。

問題內容

我發現 sqlc codegen 應用程式有問題。萬一,當我需要 interval (postgresql) 欄位時,sqlc 會產生一個帶有 int64 欄位的物件。此解決方案看起來已損壞,並在掃描行時產生錯誤: errorf("cannot conversion %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 類型,問題很快就得到了修復。 duration 和程式碼開始工作,但此程式碼是程式碼產生的,看起來是個錯誤的決定。

在嘗試透過 sqlc.yaml 配置覆蓋類型時,我什麼都沒有,物件仍在建立 int64 類型。我哪裡錯了,該如何解決這個問題?

解決方法

支援的類型,您將看到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"

提示:如果它不適用於最明顯的資料類型,您可以嘗試另一種。

以上是sqlc.yaml 配置不會覆寫 postgresql 時間間隔到 time.Duration的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除