使用 GoLang 保存额外数据
Saving Extra Data With GoLang
所以我使用 Go Lang 1.10.3 和 Echo 框架,Gorm 和 Postgres 作为我的数据库。
我有三个 tables / structs , Profile, Addresses 和 Profile_addresses
结构如下,
简介
type Profile struct {
gorm.Model
Company string `gorm:"size:255" json:"company"`
AddedDate time.Time `gorm:"type:date" json:"date_added"`
ProfileAddresses []ProfileAddress `gorm:"foreignkey:profile_fk" json:"address_id"`
}
地址
type Address struct {
gorm.Model
AddressLine1 string `gorm:"size:255" json:"line1"`
AddressLine2 string `gorm:"size:255" json:"line2"`
AddressLine3 string `gorm:"size:255" json:"line3"`
City string `gorm:"size:200" json:"city"`
Country string `gorm:"size:255" json:"country"`
Postcode string `gorm:"size:12" json:"postcode"`
ProfileAddresses []ProfileAddress `gorm:"foreignkey:address_fk"`
}
和个人资料地址
type ProfileAddress struct {
gorm.Model
Archive bool `json:"archive"`
Profile Profile
Address Address
AddressFK int`gorm:"ForeignKey:id"`
ProfileFK int`gorm:"ForeignKey:id"`
}
这些 table 都做得很好,但我现在正在尝试在创建新配置文件时将地址 ID 保存到配置文件地址 table。将数据发布到 /profile/add
(使用 Postman),包括以下数据
{
"company": "testing-000001",
"date_added": "3051-11-09T00:00:00+00:00",
"address_id": 3
}
现在我可以保存新的个人资料和新地址,但不能保存这些数据。我刚刚将 json:"address_id"`
选项添加到 Profile struct
的末尾,但这没有用。
我是这样设置的,因为一个配置文件可能有多个地址,所以需要一个包含所有地址 ID 的链接 table。我确定我可以分两步完成,例如保存配置文件,然后保存我想添加到该配置文件的地址,但我想让它工作。我也不需要创建新地址,这些已经添加到系统中了。
那我做错了什么?
欢迎任何帮助。
谢谢,
这就是我的工作方式,如果这不正确或有更好的方法,请告诉我。
所以我将 Profile
结构添加到 wrapper/nested 结构中,如下所示:
type ProfileWrapper struct {
gorm.Model
ProfileData Profile
AddressID int `json:"address_id"`
}
然后我 changed/updated 我发送到这个结构中的 json 数据到这个:
{
"ProfileData": {
"company": "testing-with-add-0001",
"date_added": "9067-11-09T00:00:00+00:00"
},
"address_id": 3
}
为了保存配置文件,我这样做了,
db.Create( &p.ProfileData )
然后我为配置文件地址信息构建了一个新结构 created/added,如下所示:
pd := structs.ProfileAddress{AddressFK: p.AddressID, ProfileFK: profileID}
db.Create( &pd )
不确定这是否是最好的方法,但它似乎有效。如果这有误,请告诉我。
谢谢。
所以我使用 Go Lang 1.10.3 和 Echo 框架,Gorm 和 Postgres 作为我的数据库。
我有三个 tables / structs , Profile, Addresses 和 Profile_addresses
结构如下,
简介
type Profile struct {
gorm.Model
Company string `gorm:"size:255" json:"company"`
AddedDate time.Time `gorm:"type:date" json:"date_added"`
ProfileAddresses []ProfileAddress `gorm:"foreignkey:profile_fk" json:"address_id"`
}
地址
type Address struct {
gorm.Model
AddressLine1 string `gorm:"size:255" json:"line1"`
AddressLine2 string `gorm:"size:255" json:"line2"`
AddressLine3 string `gorm:"size:255" json:"line3"`
City string `gorm:"size:200" json:"city"`
Country string `gorm:"size:255" json:"country"`
Postcode string `gorm:"size:12" json:"postcode"`
ProfileAddresses []ProfileAddress `gorm:"foreignkey:address_fk"`
}
和个人资料地址
type ProfileAddress struct {
gorm.Model
Archive bool `json:"archive"`
Profile Profile
Address Address
AddressFK int`gorm:"ForeignKey:id"`
ProfileFK int`gorm:"ForeignKey:id"`
}
这些 table 都做得很好,但我现在正在尝试在创建新配置文件时将地址 ID 保存到配置文件地址 table。将数据发布到 /profile/add
(使用 Postman),包括以下数据
{
"company": "testing-000001",
"date_added": "3051-11-09T00:00:00+00:00",
"address_id": 3
}
现在我可以保存新的个人资料和新地址,但不能保存这些数据。我刚刚将 json:"address_id"`
选项添加到 Profile struct
的末尾,但这没有用。
我是这样设置的,因为一个配置文件可能有多个地址,所以需要一个包含所有地址 ID 的链接 table。我确定我可以分两步完成,例如保存配置文件,然后保存我想添加到该配置文件的地址,但我想让它工作。我也不需要创建新地址,这些已经添加到系统中了。
那我做错了什么?
欢迎任何帮助。
谢谢,
这就是我的工作方式,如果这不正确或有更好的方法,请告诉我。
所以我将 Profile
结构添加到 wrapper/nested 结构中,如下所示:
type ProfileWrapper struct {
gorm.Model
ProfileData Profile
AddressID int `json:"address_id"`
}
然后我 changed/updated 我发送到这个结构中的 json 数据到这个:
{
"ProfileData": {
"company": "testing-with-add-0001",
"date_added": "9067-11-09T00:00:00+00:00"
},
"address_id": 3
}
为了保存配置文件,我这样做了,
db.Create( &p.ProfileData )
然后我为配置文件地址信息构建了一个新结构 created/added,如下所示:
pd := structs.ProfileAddress{AddressFK: p.AddressID, ProfileFK: profileID}
db.Create( &pd )
不确定这是否是最好的方法,但它似乎有效。如果这有误,请告诉我。
谢谢。