登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-04-09 08:00:37 136浏览 收藏

一分耕耘,一分收获!既然都打开这篇《无法读取“一对多”与 go-pg 的关系》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!

问题内容

我正在尝试用 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
}

如果我执行它,我总是收到错误消息 errorreadingstatemachine:model=statemachine does nothaverelation="states"

我以前做过类似的关系,他们工作了,但现在,我无法让它再次工作:(


解决方案


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

本篇关于《无法读取“一对多”与 go-pg 的关系》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>