英文:
GORM embedded struct not getting migrated to SQL
问题
我在GORM中声明了以下模型:
type DBModel struct {
ID uint `gorm:"primaryKey"`
CreatedAt *time.Time `json:"_"`
UpdatedAt *time.Time `json:"_"`
DeletedAt *time.Time `json:"_"`
ClientID uint `gorm:"not_null"`
}
type Address struct {
address string
city string
state string
pincode int
country string
}
type Office struct {
DBModel DBModel `gorm:"embedded"`
Address Address `gorm:"embedded"`
Name string
}
在运行以下代码时:
func Init(db *gorm.DB) {
DB = db
DB.AutoMigrate(&models.Office{})
}
被迁移的Office表具有以下字段:
id
created_at
updated_at
deleted_at
client_id
name
为什么Address结构体没有被嵌入?
英文:
I am declaring the following models in GORM :
type DBModel struct {
ID uint `gorm:"primaryKey"`
CreatedAt *time.Time `json:"_"`
UpdatedAt *time.Time `json:"_"`
DeletedAt *time.Time `json:"_"`
ClientID uint `gorm:"not_null"`
}
type Address struct {
address string
city string
state string
pincode int
country string
}
type Office struct {
DBModel DBModel `gorm:"embedded"`
Address Address `gorm:"embedded"`
Name string
}
On running
func Init(db *gorm.DB) {
DB = db
DB.AutoMigrate(&models.Office{})
}
The Office table being migrated has fields:
id
created_at
updated_at
deleted_at
client_id
name
Why is the Address struct not being embedded ?
答案1
得分: 0
好的,我明白了。
Address 结构体的字段应该是导出字段。
字段必须以大写字母开头才能被导出。
修改 Address 结构体如下:
type Address struct {
Address string
City string
State string
Pincode int
Country string
}
英文:
Okay understood it.
The fields of the Address struct should be exported fields.
Fields must start with capital letters to be exported.
Changing the Address struct does the work :
type Address struct {
Address string
City string
State string
Pincode int
Country string
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论