doujia6503 2018-07-23 05:20
浏览 1315
已采纳

Gorm AutoMigrate()和CreateTable()无法正常工作

I have run through the related issues here and all over on google.

I am using Gorm with SQLite3.

Whenever I try to run either function on my struct, I get an error. When I debug and step through I see table name is "". Gorm is not getting my structs name which is models.UserAuth. If I call DropTable(models.UserAuth{}) to shows there is no table named user_auth (but at least it figured out the table name). When I browse the DB, of course there, are no tables.

my struct is

type UserAuth struct {
    gorm.Model
    ProfileID int      `gorm:"not null" json:"profile_id"`
    Username  string   `gorm:"size:20;unique_index" json:"username"`
    Email     string   `gorm:"type:varchar(100);unique_index" json:"email"`
    Password  string   `gorm:"not null" json:"password"`
    Remember  bool     `gorm:"not null" json:"remeber"`
    TwoFA     bool     `gorm:"not null" json:"twofa"`
    Access    int      `gorm:"not null" json:"access"`
    State     int      `gorm:"not null" json:"state"`
    LastSeen  string   `gorm:"not null" json:"lastseen"``
}

My COMMON migrate function is:

func (d *Database) Migrate(m interface{}) {
    logEntry := fmt.Sprintf("Auto Migrating %s...", reflect.TypeOf(m))

    //d.db.DropTable(&models.UserAuth{})
    //d.db.CreateTable(&models.UserAuth{})

    // Do it the hard way
    //if d.db.HasTable(&m) == false {
    // Create table for model `User`
    //  d.db.CreateTable(&m)
    //  d.logThis.Info(fmt.Sprintf("%s %s with error %s", logEntry, "Failed", d.db.Error))
    //}

    // Migrate the schema
    db := d.db.AutoMigrate(&m) //<--- Line 84
    if db != nil && db.Error != nil {
        //We have an error
        d.logThis.Fatal(fmt.Sprintf("%s %s with error %s", logEntry, "Failed", db.Error))
    }
    d.logThis.Info(fmt.Sprintf("%s %s", logEntry, "Success"))
}

And finally here is how it is being called:

app.Db.Migrate(models.UserAuth{})

Actual output with debug on:

({PathToProject}/database/database.go:84) 
[2018-07-23 06:12:24]  near ")": syntax error 

({PathToProject}/database/database.go:84) 
[2018-07-23 06:12:24]  [0.99ms]  CREATE TABLE "" ( )  
[0 rows affected or returned ] 

FYI, the downvote was uncalled for - It is a legitimate issue as I have taken the gorm example on the document summary page and basically just changed the struct. And the struct is using the correct basic types (except for the slice that made it back into the code on the original post) and the error is not very helpful. I see there is a SYNTAX error before the blank table name error - but why? Did I give GORM a valid struct?

  • 写回答

1条回答 默认 最新

  • duanhuang2804 2018-07-23 06:27
    关注

    I am pretty sure that sqlite does not have a type for your AuthIPs ([]string). I am not sure if GORM allows for you to write custom Valuer and Scanner interface methods that would allow you to convert a string to an array and back again or not, but it's something you might want to check out.

    Update: Change db := d.db.AutoMigrate(&m) to db := d.db.AutoMigrate(m) to allow for the reflection to get the type name.

    If you implement the tabler interface you can also better control the name of the table.

    https://github.com/jinzhu/gorm/blob/master/scope.go#L305

    type tabler interface {
        TableName() string
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)