Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make multiple models auto migrate in gorm

Tags:

go

go-gorm

go-gin

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 ?

like image 278
temps hd Avatar asked Oct 18 '25 07:10

temps hd


1 Answers

One option is to nest the structs inside the AutoMigrate function:

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

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

var models = []interface{}{&User{}, &Product{}, &Order{}}

db.Automigrate(models...)
like image 195
dave Avatar answered Oct 20 '25 07:10

dave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!