douzhang2092 2018-01-20 18:07
浏览 16

政府和协会

I'm trying to figure out how to load associations with gorm. The documentation is not that great.

I have to models:

type Account struct {
    gorm.Model
    Name          string `gorm:"column:name;unique_index;not null;size:255"`
    AccountTypeID uint   `gorm:"not null"`
    UserID        uint   `gorm:"not null"`
    AccountType   accounttypes.AccountType
}

and

type AccountType struct {
    gorm.Model
    Name string `gorm:"column:name;unique_index;not null;size:255"`
    Type string `gorm:"column:type;unique_index;not null;size:255"`
}

So every Account has a Account Type associated with it.

When I try loading the data via:

func GetAccounts(userId uint) ([]Account, error) {
    db := common.GetDB()
    var model []Account
    db.LogMode(true)
    err := db.Model(&Account{}).Where("accounts.user_id =?", userId).Related(&accounttypes.AccountType{}, "AccountTypes").Error
    return model, err
}

This is the output: enter image description here

Could someone please explain how to do this properly the 'gorm way'?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)