Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automigrate all structs automatically #38

Closed
leebrooks0 opened this issue Jan 2, 2014 · 3 comments
Closed

Automigrate all structs automatically #38

leebrooks0 opened this issue Jan 2, 2014 · 3 comments

Comments

@leebrooks0
Copy link
Contributor

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

@jinzhu
Copy link
Member

jinzhu commented Jan 2, 2014

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(), "...")
        }
    }
}

@jinzhu jinzhu closed this as completed Jan 2, 2014
@leebrooks0
Copy link
Contributor Author

Thanks for the tip!

On Thu, Jan 2, 2014 at 1:49 PM, Jinzhu notifications@github.com 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
.

@ernestzhang-11
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants