簡體   English   中英

無法讀取與 go-pg 的“一對多”關系

[英]Cannot read “one to many” Relation with go-pg

我正在嘗試使用 go 實現一個小狀態機並將我的狀態存儲在 postgres 數據庫中。

我像這樣創建了我的數據庫:

CREATE TABLE state_machines
(
   id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
   initial_state TEXT NOT NULL,
   "name" TEXT NOT NULL
);

CREATE TABLE state_machine_states
(
   state_machine_id uuid NOT NULL REFERENCES state_machines(id) ON DELETE CASCADE,
   "name" TEXT NOT NULL,
   PRIMARY KEY(state_machine_id, "name")
);
// StateMachine is the DB schema
type StateMachine struct {
    ID           *uuid.UUID                `pg:"id,pk,type:uuid,default:uuid_generate_v4()"`
    Name         string                    `pg:"name"`
    InitialState string                    `pg:"initial_state"`
    States       []*StateMachineState      `pg:"fk:state_machine_id"`
}

// StateMachineState is the DB schema
type StateMachineState struct {
    StateMachineID uuid.UUID `pg:"state_machine_id,fk"`
    Name           string    `pg:"name"`
}

我正在使用 go-pg 9.2,我正在嘗試從“狀態”關系加載狀態機及其狀態列表。

我加載狀態機的函數如下所示:

func (cep *repository) GetStateMachines() ([]*StateMachine, error) {
    stateMachines := []*StateMachine{}

    err := cep.db.Model(&stateMachines).
        Relation("States").
        Relation("Transitions").
        Select()

    return stateMachines, err
}

如果我執行它,我總是收到錯誤消息Error reading state machine: model=StateMachine does not have relation="States"

我以前做過類似的關系,他們工作了,現在,我無法讓它再次工作:(

嘗試升級到完全支持關系的 v10: https : //pg.uptrace.dev/orm/has-many-relation/

查看 go-pg 中是否有 .Debug() 函數進行查詢。 我使用https://gorm.io/ ,並且有一個 Debug 函數,它返回所有 SQL 查詢。 當我看到發送了什么查詢時,我登錄 postgresql 並手動嘗試它們以查看更詳細的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM