Gorm 多张表的自动迁移

db.AutoMigrate(&Address{}, &Apply{}, &Banner{}, &Booth{}, &Category{}, &Comment{}, &Coupon{}, &Dictionary{}, &Group{}, &Img{}, &Order{}, &Product{}, &Tag{}, &Topic{}, &User{})

 

给每张表添加表注释

db.Set("gorm:table_options", "comment '用户地址表' ").AutoMigrate(&Address{})

  

像这种一个个写。

 

当然更好的方式是用循环

 

type modelAuto struct {
		Model   interface{}
		Comment string
	}

	models := []modelAuto{
		modelAuto{&Address{}, "1111"},
		modelAuto{&Apply{}, "2222"},
	}

	for _, val := range models {
		fmt.Println(val.Model)
		db.Set("gorm:table_options", "comment '"+val.Comment+"'").AutoMigrate(val.Model)
	}

  

posted @ 2021-05-28 15:08  winyh  阅读(568)  评论(0编辑  收藏  举报