在 beego orm 中插入带有 m2m 的模型
Insert for model with m2m in beego orm
我有两个模型:
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
}
我正在尝试使用此代码与一名玩家一起创建新游戏:
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)
}
}
但它只是通过 "game_players" table 自动创建的 orm.RunSyncdb()
.
为游戏和玩家创建了两个没有 rel-connection 的插入
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 (, , ) 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 (, ) RETURNING "id"] - `2016-09-29 22:19:59.8725853 +1000 VLAT`, `2016-09-29 22:19:59.8725853 +1000 VLAT`
我在 docs 中找不到使用 m2m 模型的任何特殊规则并向社区寻求帮助。我应该如何在 table 中插入新行?
根据this,你必须在创建对象游戏后创建一个 m2m 对象,如下所示:
m2m := models.ORM.QueryM2M(&game, "Players")
而不是 game.Players = []*models.Player{&playerA}
,你写:
num, err := m2m.Add(playerA)
因此,您的函数必须如下所示:
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)
}
}
希望对您有所帮助。
P.S.: 顺便说一句,你是对的,没有必要指定 m2m 的名称 table.
我有两个模型:
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
}
我正在尝试使用此代码与一名玩家一起创建新游戏:
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)
}
}
但它只是通过 "game_players" table 自动创建的 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 (, , ) 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 (, ) RETURNING "id"] - `2016-09-29 22:19:59.8725853 +1000 VLAT`, `2016-09-29 22:19:59.8725853 +1000 VLAT`
我在 docs 中找不到使用 m2m 模型的任何特殊规则并向社区寻求帮助。我应该如何在 table 中插入新行?
根据this,你必须在创建对象游戏后创建一个 m2m 对象,如下所示:
m2m := models.ORM.QueryM2M(&game, "Players")
而不是 game.Players = []*models.Player{&playerA}
,你写:
num, err := m2m.Add(playerA)
因此,您的函数必须如下所示:
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)
}
}
希望对您有所帮助。
P.S.: 顺便说一句,你是对的,没有必要指定 m2m 的名称 table.