Gorm: Automigrate all structs automatically

Created on 2 Jan 2014  ·  3Comments  ·  Source: go-gorm/gorm

Is it possible to automigrate all structs automatically?

Currently it seems like the name of each struct to migrate needs to be passed to this function https://github.com/jinzhu/gorm/blob/690cb1430c2e27011324c51826301a7daf728e65/main.go#L261

Most helpful comment

Because gorm can't know which struct you want to migrate, so can't do this automatically.

But I am doing something like this in my application.

package main

import (
    "fmt"
    . "xxx.com/xxxx/xxxx/app/models"
    . "xxx.com/xxxx/xxxx/db"
    "reflect"
)

func main() {
    for _, model := range []interface{}{
        Payment{}, Invoice{}, Transaction{},
        SubscriptionService{}, SubscriptionBenefit{}, Subscription{},
        Address{}, User{}, UserService{},
        Service{},
    } {
        if err := DB.AutoMigrate(model).Error; err != nil {
            fmt.Println(err)
        } else {
            fmt.Println("Auto migrating", reflect.TypeOf(model).Name(), "...")
        }
    }
}

All 3 comments

Because gorm can't know which struct you want to migrate, so can't do this automatically.

But I am doing something like this in my application.

package main

import (
    "fmt"
    . "xxx.com/xxxx/xxxx/app/models"
    . "xxx.com/xxxx/xxxx/db"
    "reflect"
)

func main() {
    for _, model := range []interface{}{
        Payment{}, Invoice{}, Transaction{},
        SubscriptionService{}, SubscriptionBenefit{}, Subscription{},
        Address{}, User{}, UserService{},
        Service{},
    } {
        if err := DB.AutoMigrate(model).Error; err != nil {
            fmt.Println(err)
        } else {
            fmt.Println("Auto migrating", reflect.TypeOf(model).Name(), "...")
        }
    }
}

Thanks for the tip!

On Thu, Jan 2, 2014 at 1:49 PM, Jinzhu [email protected] wrote:

Closed #38 https://github.com/jinzhu/gorm/issues/38.


Reply to this email directly or view it on GitHubhttps://github.com/jinzhu/gorm/issues/38
.

Now, do you have a new method to solve it ?
Thanks. @jinzhu

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pjebs picture pjebs  ·  3Comments

koalacxr picture koalacxr  ·  3Comments

bramp picture bramp  ·  3Comments

alanyuen picture alanyuen  ·  3Comments

zeropool picture zeropool  ·  3Comments