Gorm UUID外键 – 属于

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

Gorm UUID foreign key - belongs to

问题

我正在尝试在两个数据库表之间创建一个属于关系,使用GORM,我的代码如下:

type Shop struct {
	ID    uuid.UUID `json:"id" gorm:"primaryKey;type:uuid"`
	Name  string    `json:"name" gorm:"not null" validate:"required"`
	City  string    `json:"city" gorm:"not null" validate:"required"`
	State string    `json:"state" gorm:"not null" validate:"required"`
}
type Employee struct {
	ID        uuid.UUID `json:"id" gorm:"primaryKey;type:uuid"`
	FirstName string    `json:"first_name" gorm:"not null" validate:"required"`
	LastName  string    `json:"last_name" gorm:"not null" validate:"required"`
	Email     string    `json:"email" gorm:"not null;unique" validate:"required,email"`
	Password  string    `json:"password" gorm:"not null" validate:"required"`
	Active    bool      `json:"active" gorm:"not null;default:false"`
	ShopId    uuid.UUID `json:"shop_id" gorm:"type:uuid"`
	Shop      Shop      `gorm:"foreignKey:ShopID"`
}

当我运行迁移时,出现以下错误:

[error] invalid field found for struct .../.../.../api/models.Employee's field Shop: define a valid foreign key for relations or implement the Valuer/Scanner interface

我找到了一些使用数字主键的参考资料,它们似乎工作正常,但我找不到使用UUID的解决方案...

英文:

I'm trying to create a belongs to relation between two database tables, using GORM, my code is the following:

type Shop struct {
	ID    uuid.UUID `json:"id" gorm:"primaryKey;type:uuid"`
	Name  string    `json:"name" gorm:"not null" validate:"required"`
	City  string    `json:"city" gorm:"not null" validate:"required"`
	State string    `json:"state" gorm:"not null" validate:"required"`
}
type Employee struct {
	ID        uuid.UUID `json:"id" gorm:"primaryKey;type:uuid"`
	FirstName string    `json:"first_name" gorm:"not null" validate:"required"`
	LastName  string    `json:"last_name" gorm:"not null" validate:"required"`
	Email     string    `json:"email" gorm:"not null;unique" validate:"required,email"`
	Password  string    `json:"password" gorm:"not null" validate:"required"`
	Active    bool      `json:"active" gorm:"not null;default:false"`
	ShopId    uuid.UUID `json:"shop_id" gorm:"type:uuid"`
	Shop      Shop      `gorm:"foreignKey:ShopID"`
}

When I run the migrations, this error pops up:

[error] invalid field found for struct .../.../.../api/models.Employee's field Shop: define a valid foreign key for relations or implement the Valuer/Scanner interface

I've found some references using number primary keys and they seem to work fine, but I can't find any solution to work with uuids...

答案1

得分: 1

我不确定,但我理解错误消息的意思是类型uuid.UUID没有实现接口Valuer和Scanner的方法。

你应该创建自己的UUID类型,可以像这样:

type UUID uuid.UUID

func (id UUID) Value() (driver.Value, error) {	
	return id.String(), nil
}

func (id *UUID) Scan(value interface{}) error {
	dbID, ok := value.(string)
	if !ok {
		return errors.New("id scan: invalid value")
	}
    
	*id = uuid.MustParse(dbID)
	return nil
}

然后在你的结构体定义中使用它:

type Shop struct {
    ID    UUID `json:"id" gorm:"primaryKey;type:uuid"`
    //...
}
英文:

I'm not sure, but what I understand of the message error is that the type uuid.UUID doesn't have implemented the methods for the interfaces Valuer and Scanner.

You should create your own type UUID, which can be something like this:

type UUID uuid.UUID

func(id UUID) Value() (driver.Value, error) {	
	return id.String(), nil
}

func (id *UUID) Scan(value interface{}) error {
	dbID, ok := value.(string)
	if !ok {
		return errors.New("id scan: invalid value")
	}
    
    
	*e = uuid.MustParse(dbID)
	return nil
}

And use it on your struct's definitions:

type Shop struct {
    ID    UUID `json:"id" gorm:"primaryKey;type:uuid"`
    //...
}

huangapple
  • 本文由 发表于 2022年5月5日 01:52:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/72117428.html
匿名

发表评论

匿名网友

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

确定