Golang Gorm迁移结构体时出错。

huangapple go评论93阅读模式
英文:

Golang Gorm error migrating struct

问题

我在我的小应用程序中有一个小错误,我真的不知道它是从哪里来的。所以我有4个结构体,其中一个结构体有几个一对一的关系。

我连接到数据库并使用automigrate来迁移我的4个结构体并创建必要的表。
问题出在这一点上,它在数据库中没有创建任何内容,在终端中我收到以下消息:
(错误1060:'id'字段的名称已经在使用中)

我的代码
main.go

package main

import (
    "fmt"
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/mysql"
)

var db *gorm.DB
var err error

const (
    mysupersecretpassword = "cr9ih_pvr9f9kc75n#bz&y%(@+^&1_#hr0^)-$kv%n3dh84$^w"
)

func main() {

    db, err = gorm.Open("mysql", "root:root@/test?charset=utf8&parseTime=True")
    if err != nil {
        fmt.Println(err)
    }
    defer db.Close()
    db.AutoMigrate(&User{}, &Ads{}, &Type{}, &Category{}, &Location{})

}

models.go

package main

import (
    "github.com/jinzhu/gorm"
)

type User struct {
    gorm.Model
    Username string `json:"username"`
    Email    string `json:"email" form:"email"`
    Password string `json:"password" form:"password"`
    active   bool   `json:"active" gorm:"default:0"`
    level    bool   `json:"level" gorm:"default:0"`
}

type Type struct {
    gorm.Model
    Name string `json:"name" form:"name"`
}

type Category struct {
    gorm.Model
    CatID uint   `json:"category-parent" form:"category-parent" gorm:"default:0"`
    Name  string `json:"name" form:"name"`
}

type Location struct {
    gorm.Model
    Location string `json:"location" form:"location"`
}

type Ads struct {
    gorm.Model
    User     User     `json:"user"`
    Type     Type     `json:"type" form:"type"`
    Category Category `json:"category" form:"category"`
    Title    string   `json:"title" form:"title"`
    Content  string   `json:"content" form:"content"`
    Location Location `json:"location" form:"location"`
}

等待一个能让我走上正确道路的答案 Golang Gorm迁移结构体时出错。

英文:

I have a small mistake in my small application and I really do not see where it can come from. So I have 4 structs, one of the 4 struct has several one-to-one relationships.

I connect to my database and use automigrate to migrate my 4 structs and create the necessary tables.
The problem is at this point, it does not create anything in the database and in the terminal I have this message:
(Error 1060: Name of the 'id' field already in use)

My code
main.go

package main

import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)

var db *gorm.DB
var err error

const (
mysupersecretpassword = "cr9ih_pvr9f9kc75n#bz&y%(@+^&1_#hr0^)-$kv%n3dh84$^w"
)

func main() {

db, err = gorm.Open("mysql", "root:root@/test?charset=utf8&parseTime=True")
if err != nil {
	fmt.Println(err)
}
defer db.Close()
db.AutoMigrate(&User{}, &Ads{}, &Type{}, &Category{}, &Location{})

}

models.go

package main

import (
    "github.com/jinzhu/gorm"
)

type User struct {
    gorm.Model
    Username string `json:"username"`
    Email    string `json:"email" form:"email"`
    Password string `json:"password" form:"password"`
    active   bool   `json:"active" gorm:"default:0"`
    level    bool   `json:"level" gorm:"default:0"`
}

type Type struct {
    gorm.Model
    Name string `json:"name" form:"name"`
}

type Category struct {
    gorm.Model
    CatID uint   `json:"category-parent" form:"category-parent" gorm:"default:0"`
    Name  string `json:"name" form:"name"`
}

type Location struct {
    gorm.Model
    Location string `json:"location" form:"location"`
}

type Ads struct {
    gorm.Model
    User     User     `json:"user"`
    Type     Type     `json:"type" form:"type"`
    Category Category `json:"category" form:"category"`
    Title    string   `json:"title" form:"title"`
    Content  string   `json:"content" form:"content"`
    Location Location `json:"location" form:"location"`
}

Waiting for an answer that could put me on the right path Golang Gorm迁移结构体时出错。

答案1

得分: 1

"AutoMigrate只会创建表、缺失的列和缺失的索引,不会更改现有列的类型或删除未使用的列以保护您的数据。

我猜测您的其中一个表已经存在,并且该表中的'id'列与'gorm.Model'要创建的类型不同。您可以通过执行以下操作找出是哪个表:

db.AutoMigrate(&User{})
db.AutoMigrate(&Ads{})
db.AutoMigrate(&Type{})
db.AutoMigrate(&Category{})
db.AutoMigrate(&Location{})

然后查看失败的位置。然后,为了安全起见,我会备份该表,然后要么完全删除该表,要么将'id'列重命名为'tmp_id',看看automigrate是否可以修复它,如果可以修复,然后删除'tmp_id'列。"

英文:

> AutoMigrate will ONLY create tables, missing columns and missing indexes, and WON'T change existing column's type or delete unused columns to protect your data.

I would guess that one of your tables already exists, and the id column in that table is a different type than what gorm.Model wants to create. I would figure out which table it is by doing:

db.AutoMigrate(&User{})
db.AutoMigrate(&Ads{})
db.AutoMigrate(&Type{})
db.AutoMigrate(&Category{})
db.AutoMigrate(&Location{})

and seeing where it fails. Then, I would backup that table (just in case), and then either just drop the table completely, or rename the id column to tmp_id, see if automigrate fixes it, and if so, drop the tmp_id column.

答案2

得分: 1

我已经发现,如果Gorm在用户权限上存在问题,它将不会记录正确的错误信息或任何错误。例如,当以root身份登录时,表是存在的,但是我登录的用户根本看不到它,Gorm只是在迁移过程中运行,而不创建表或修改它(即使它是不同的模式),而且没有报告任何权限问题。

英文:

FYI I've found Gorm will not log proper error or any error if it has permission issues on the user. For example the table existed when logged in as root, but the user I was logging in as didn't see it at all, Gorm just ran through Migration without creating the table or altering it (even though it was different schema) just didn't report any permission issue at all.

huangapple
  • 本文由 发表于 2017年1月4日 04:02:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/41451431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定