douhui3760 2019-09-13 17:44
浏览 231
已采纳

如何使多个模型在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 2019-09-13 19:03
    关注

    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...)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于模型训练的一个问题
  • ¥15 装了几千台服务器从来没遇到这种问题,哎看下哪位帮我解决吧
  • ¥15 单片机程序上的困难问题
  • ¥15 请教某软件缓存Ts文件破解合并mp4的方法
  • ¥15 求小游戏炸弹人中关于敌人的C++代码
  • ¥15 拿到这个服务器最高权限有偿
  • ¥50 来个抓app跳转支付宝转链接的
  • ¥15 remotes安装提示没有description文件
  • ¥15 AttributeError: 'NoneType' object has no attribute 'drop_duplicates'报错
  • ¥15 以下代码,运行结果报错