douhui3760
douhui3760
2019-09-13 17:44

如何使多个模型在gorm中自动迁移

已采纳

i can see the documentation we do automigrate like this, db.AutoMigrate(&model.TheTodo{})

how about if we have a lot of multiples models? db.AutoMigrate(&model.TheTodo{}, &model.TheBlog{}, &model.Employee{}, and many more...... )

will gorm create that table if we put like that? and is that any way to make inside AutoMigrate to make short?

db.AutoMigrate(allmodels)

would it possible ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douji1077 douji1077 2年前

    You can do:

    db.AutoMigrate(
        &User{}, 
        &Product{},
        &Order{},
    )
    

    If you want to make the inside "short", you could do:

    var models := []interface{&User{}, &Product{}, &Order{}}
    
    db.Automigrate(models...)
    
    点赞 评论 复制链接分享

相关推荐