doulei3488 2016-09-29 12:30
浏览 141
已采纳

在beego orm中插入M2m模型

I have two models:

type MainFields struct {
        Id int `orm:"auto"`
        Created time.Time `orm:"auto_now_add;type(datetime)"`
        Updated time.Time `orm:"auto_now;type(datetime)"`
    }

type Game struct {
    MainFields
    Players  []*Player `orm:"rel(m2m)"`
}

type Player struct {
    MainFields
    Games []*Game `orm:"reverse(many)"`
    NickName string
}

And with this code i`am trying to create new game with one player:

func insertTestData() {
    var playerA models.Player
    playerA.NickName = "CoolDude"
    id, err := models.ORM.Insert(&playerA)
    if err != nil {
        log.Printf(err.Error())
    } else {
        log.Printf("Player ID: %v", id)
    }

    var game models.Game
    game.Players = []*models.Player{&playerA}
    id, err = models.ORM.Insert(&game)
    if err != nil {
        log.Printf(err.Error())
    } else {
        log.Printf("Game ID: %v", id)
    }

}

But it just create two inserts for game and player without rel-connection through "game_players" table which created automatically with orm.RunSyncdb().

2016/09/29 22:19:59 Player ID: 1
[ORM]2016/09/29 22:19:59  -[Queries/default] - [  OK / db.QueryRow /    11.0ms] - [INSERT INTO "player" ("created", "updated", "nick_name") VALUES ($1, $2, $3) RETURNING "id"] - `2016-09-29 22:19:59.8615846 +1000 VLAT`, `2016-09-29 22:19:59.8615846 +1000 VLAT`, `CoolDude`
2016/09/29 22:19:59 Game ID: 1
[ORM]2016/09/29 22:19:59  -[Queries/default] - [  OK / db.QueryRow /    11.0ms] - [INSERT INTO "game" ("created", "updated") VALUES ($1, $2) RETURNING "id"] - `2016-09-29 22:19:59.8725853 +1000 VLAT`, `2016-09-29 22:19:59.8725853 +1000 VLAT`

I can`t find any special rules for working with m2m-models in docs and ask for help to community. How should i insert new row in table?

  • 写回答

1条回答 默认 最新

  • doumu4032 2016-09-29 17:08
    关注

    According to this, you have to make a m2m object, after creating object game, like this:

    m2m := models.ORM.QueryM2M(&game, "Players")
    

    And instead of game.Players = []*models.Player{&playerA}, you write:

    num, err := m2m.Add(playerA)
    

    So, your function must look like this:

    func insertTestData() {
        var playerA models.Player
        playerA.NickName = "CoolDude"
        id, err := models.ORM.Insert(&playerA)
        if err != nil {
            log.Printf(err.Error())
        } else {
            log.Printf("Player ID: %v", id)
        }
    
        var game models.Game
        id, err = models.ORM.Insert(&game)
        if err != nil {
            log.Printf(err.Error())
        } else {
            log.Printf("Game ID: %v", id)
        }
    
        m2m := o.QueryM2M(&game, "Players")
        num, err := m2m.Add(playerA)
        if err == nil {
            log.Printf("Added nums: %v", num)
        }
    }
    

    I hope this helps.

    P.S.: BTW, you were right, It wasn't necessary to specify the name of the m2m table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题