英文:
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的默认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:
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 containsID
field in it. Reference - Second point is according to your description,
store
(location) has one to
many relationship withdrink
. That means a store can have multiple
drinks but a drink should belong to only one store. Inone-to-many
relationship there should be a reference or relation id in themany
side. That means in your case indrink
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论