登录
首页 >  Golang >  Go问答

如何在beego orm中将结构中的特定键设置为外键?

来源:stackoverflow

时间:2024-04-14 13:57:18 216浏览 收藏

哈喽!大家好,很高兴又见面了,我是golang学习网的一名作者,今天由我给大家带来一篇《如何在beego orm中将结构中的特定键设置为外键?》,本文主要会讲到等等知识点,希望大家一起学习进步,也欢迎大家关注、点赞、收藏、转发! 下面就一起来看看吧!

问题内容

我想将coins结构体的地址设置为accounts结构体地址的外键,如何在beego orm中设置?

type Coins struct {
    Id        int       `orm:"auto"`
    Address   string    `orm:"rel(fk);on_delete(cascade);on_update(cascade);index" json:"address"`
    Symbol    string    `json:"symbol"`
    Amount    float64   `orm:"digits(64);decimals(6)" json:"amount"`
    CreatedAt time.Time `orm:"auto_now_add;type(datetime)"`
    UpdatedAt time.Time `orm:"auto_now_add;type(datetime)"`
}

type Accounts struct {
    Id                       int       `orm:"auto"`
    Address                  string    `orm:"index;unique" json:"address"`
    Type                     string    `json:"type"`
}

解决方案


看看这是否有效:

type Coins struct {
    Id        int       `orm:"auto"`
    Accouts   *Accounts `orm:"rel(fk)"`
    Address   string `orm:"pk;auto;on_delete(cascade);on_update(cascade);index" json:"address"`
    Symbol    string    `json:"symbol"`
    Amount    float64   `orm:"digits(64);decimals(6)" json:"amount"`
    CreatedAt time.Time `orm:"auto_now_add;type(datetime)"`
    UpdatedAt time.Time `orm:"auto_now_add;type(datetime)"`
}

type Accounts struct {
    Id                       int       `orm:"pk;auto"`
    Address                  string    `orm:"index;unique" json:"address"`
    Type                     string    `json:"type"`
}

理论要掌握,实操不能落!以上关于《如何在beego orm中将结构中的特定键设置为外键?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

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