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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题