简体   繁体   English

无法读取与 go-pg 的“一对多”关系

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

I am trying to implement a little state machine with go and store my states in a postgres db.我正在尝试使用 go 实现一个小状态机并将我的状态存储在 postgres 数据库中。

i created my database like this:我像这样创建了我的数据库:

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"`
}

I am using go-pg 9.2 and i am trying to load a state machine and a list of its states from the "States" relation.我正在使用 go-pg 9.2,我正在尝试从“状态”关系加载状态机及其状态列表。

My function to load the state machines looks like this:我加载状态机的函数如下所示:

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

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

    return stateMachines, err
}

If I execute it, I always get the error message Error reading state machine: model=StateMachine does not have relation="States"如果我执行它,我总是收到错误消息Error reading state machine: model=StateMachine does not have relation="States"

I have done similar relations before and they worked and now, I cannot get it to work again :(我以前做过类似的关系,他们工作了,现在,我无法让它再次工作:(

尝试升级到完全支持关系的 v10: https : //pg.uptrace.dev/orm/has-many-relation/

See if there is .Debug() function in go-pg for a query.查看 go-pg 中是否有 .Debug() 函数进行查询。 I use https://gorm.io/ , and there is a Debug funcion, that returns all of the SQL queries.我使用https://gorm.io/ ,并且有一个 Debug 函数,它返回所有 SQL 查询。 When i see what queries are send, i log in postgresql and try them manually to see a more detailed error.当我看到发送了什么查询时,我登录 postgresql 并手动尝试它们以查看更详细的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM