GORM外键似乎没有添加正确的字段。

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

GORM foreign key doesn't seem to add proper fields

问题

我有以下模型:

type Drink struct {
	gorm.Model           // 向表中添加一些元数据字段
	ID         uuid.UUID `gorm:"type:uuid;primary key"`
	Name       string    `gorm:"index;not null;"`
	Volume     float64   `gorm:"not null;type:decimal(10,2)"`
	ABV        float64   `gorm:"not null;type:decimal(10,2);"`
	Price      float64   `gorm:"not null;type:decimal(10,2);"`
	Location   Location  `gorm:"ForeignKey:DrinkID;"`
}

type Location struct {
	gorm.Model           // 向表中添加一些元数据字段
	ID         uuid.UUID `gorm:"primary key;type:uuid"`
	DrinkID    uuid.UUID
	Name       string `gorm:"not null;"`
	Address    string `gorm:"not null;type:decimal(10,2)"`
	Phone      int    `gorm:"not null;type:decimal(10,0);"`
}

然而,当我运行程序时,它会添加两个表,但是在Drink表中没有Location字段。
无论我之前是否删除了表,我的数据库在迁移后看起来都是这样的:
GORM外键似乎没有添加正确的字段。

我有一种隐隐的感觉,可能是因为我没有使用gorm的默认ID,但如果是这样的话,有人能指导我如何以正确的方式用UUID覆盖默认的ID吗?或者如果这不是问题,我已经在这个问题上工作了几天,我真的不想走"简单"的路线,只使用gorm提供的默认设置,我实际上想理解这里发生了什么,以及如何正确地做我想做的事情。当我运行API时没有出现任何错误,迁移似乎也运行成功了,只是我定义的字段实际上没有显示在数据库中,这意味着前端无法正确地添加数据。

我希望发生的是,在前端中有一个商店列表可供选择,当用户添加一种饮料时,他们必须从那个商店列表中选择。每个添加的饮料应该只有一个商店,因为不同商店的饮料价格是不同的。因此,在饮料表中实际上会有许多"重复"的饮料,但与不同的位置相关联。

英文:

I have the following model:

type Drink struct {
	gorm.Model           // Adds some metadata fields to the table
	ID         uuid.UUID `gorm:"type:uuid;primary key"`
	Name       string    `gorm:"index;not null;"`
	Volume     float64   `gorm:"not null;type:decimal(10,2)"`
	ABV        float64   `gorm:"not null;type:decimal(10,2);"`
	Price      float64   `gorm:"not null;type:decimal(10,2);"`
	Location   Location  `gorm:"ForeignKey:DrinkID;"`
}

type Location struct {
	gorm.Model           // Adds some metadata fields to the table
	ID         uuid.UUID `gorm:"primary key;type:uuid"`
	DrinkID    uuid.UUID
	Name       string `gorm:"not null;"`
	Address    string `gorm:"not null;type:decimal(10,2)"`
	Phone      int    `gorm:"not null;type:decimal(10,0);"`
}

however, when I run the program, it adds both tables, however there is no location field in the Drink table.
My database looks like this after the migrations, regardless of whether I drop the tables previously:
GORM外键似乎没有添加正确的字段。

I have a sneaking feeling it might be because I am not using the gorm default ID, but if that's the case can anyone point me to how to override the default ID with a UUID instead of a uint the proper way? or if that's not even the issue, please, I've been working on this for a few days now and I really don't want to take the "easy" road of just using the defaults gorm provides, I actually want to understand what is going on here and how to properly do what I am trying to do. I am getting no errors when running the API, and the migration appears to run as well, it's just the fields I have defined are not actually showing up in the database, which means that the frontend won't be able to add data properly.

What I WANT to happen here is that a list of stores will be available in the front-end, and when a user adds a drink, they will have to select from that list of stores. Each drink added should only have 1 store, as the drinks prices at different stores would be different. So technically there would be many "repeated" drinks in the drink table, but connected to different Locations.

答案1

得分: 3

  • 第一点是,由于您正在使用自定义主键,所以不应该使用gorm.Model,因为它包含了ID字段。参考链接
  • 第二点是根据您的描述,store(位置)与drink之间是一对多的关系。这意味着一个店铺可以有多种饮料,但一种饮料只能属于一个店铺。在一对多的关系中,的一方应该有一个参考或关联的ID。也就是说,在您的情况下,在drink表中需要有一个字段来引用店铺的ID。然后您的结构体应该如下所示:

MyModel 结构体

type MyModel struct {
  CreatedAt time.Time
  UpdatedAt time.Time
  DeletedAt gorm.DeletedAt `gorm:"index"`
}

Location 结构体 (Store)

type Location struct {
    MyModel
    ID         uuid.UUID `gorm:"primary key;type:uuid"`
    // 其他字段...
    Drinks     []Drink 
}

Drink 结构体

type Drink struct {
    MyModel
    ID         uuid.UUID `gorm:"type:uuid;primary key"`
    // 其他字段...
    LocationID   uuid.UUID// 这是重要的字段
}

然后,gorm会自动将drink表中的LocationID字段与Location表的ID字段关联起来。您还可以在Location结构体的Drinks数组字段中使用gorm:"foreignKey:LocationID;references:ID"来明确告诉gorm这一点。

参考链接

英文:
  • First point is as you are using custom primary key, you should not use gorm.Model as it contains ID field in it. Reference
  • Second point is according to your description, store (location) has one to
    many relationship with drink. That means a store can have multiple
    drinks but a drink should belong to only one store. In one-to-many
    relationship there should be a reference or relation id in the many
    side. That means in your case in drink table. Then your struct
    should look like this:

MyModel Struct

type MyModel struct {
  CreatedAt time.Time
  UpdatedAt time.Time
  DeletedAt gorm.DeletedAt `gorm:"index"`
}

Location Struct (Store)

type Location struct {
    MyModel
    ID         uuid.UUID `gorm:"primary key;type:uuid"`
    // other columns...
    Drinks     []Drink 
}

Drink Struct

type Drink struct {
    MyModel
    ID         uuid.UUID `gorm:"type:uuid;primary key"`
    //other columns...
    LocationID   uuid.UUID// This is important
}

Then gorm will automatically consider LocationID in drink table will be referring the ID field of Location Table. You can also explicitly instruct this to gorm using gorm:"foreignKey:LocationID;references:ID" in Location struct's Drinks array field.

Reference

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

发表评论

匿名网友

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

确定