英文:
invalid field found for struct while automigrating
问题
当我尝试根据我的结构自动迁移表时,我遇到了这个错误,我不知道为什么会出现这个错误:
解析值失败 &models.Model{ID:0x0, CreatedAt:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), UpdatedAt:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), DeletedAt:nil, DogData:[]models.DogData(nil)},出现错误:找到了无效的字段,用于结构体github.com/dog-page/models.Model的字段DogData:为关系定义一个有效的外键或实现Valuer/Scanner接口。
我是一个新手,对于使用gorm为原始JSON数据创建结构体,特别是在golang语言中,我不太了解。这里是我使用gorm的结构体的意图:
type Model struct {
ID uint `gorm:"primarykey:id" json:"id:_id"`
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
DeletedAt *time.Time `gorm:"column:deleted_at" json:"deleted_at"`
DogData []DogData
}
type DogData struct {
DogDataID uint
Name string `json:"name"`
Life_Span string `json:"life_span"`
Temperaments string `json:"temperament"`
Weight datatypes.JSON `json:"weight"`
Height datatypes.JSON `json:"height"`
Image datatypes.JSON `json:"image"`
}
type Weight struct {
Imperial string `json:"imperial"`
Metric string `json:"metric"`
}
type Height struct {
Imperial string `json:"imperial"`
Metric string `json:"metric"`
}
type Image struct {
URL string `json:"url"`
}
希望这可以帮助到你。
英文:
I am gettin this error when i try to automigrate my tables based on my structs i dont know why im gettin this error
> failed to parse value &models.Model{ID:0x0, CreatedAt:time.Date(1,
> time.January, 1, 0, 0, 0, 0, time.UTC), UpdatedAt:time.Date(1,
> time.January, 1, 0, 0, 0, 0, time.UTC), DeletedAt:<nil>,
> DogData:[]models.DogData(nil)}, got error invalid field found for
> struct github.com/dog-page/models.Model's field DogData: define a
> valid foreign key for relations or implement the Valuer/Scanner
> interface
im new at golang languague and gorm specificly when it comes to create struct for raw json data here are my intention of a struct using gorm:
type Model struct {
ID uint `gorm:"primarykey:id" json:"id:_id"`
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
DeletedAt *time.Time `gorm:"column:deleted_at" json:"deleted_at"`
DogData []DogData
}
type DogData struct {
DogDataID uint
Name string `json:"name"`
Life_Span string `json:"life_span"`
Temperaments string `json:"temperament"`
Weight datatypes.JSON `json:"weight"`
Height datatypes.JSON `json:"height"`
Image datatypes.JSON `json:"image"`
}
type Weight struct {
Imperial string `json:"imperial"`
Metric string `json:"metric"`
}
type Height struct {
Imperial string `json:"imperial"`
Metric string `json:"metric"`
}
type Image struct {
URL string `json:"url"`
}
答案1
得分: 1
模型的字段DogData:为关系定义一个有效的外键
模型与DogData之间存在一对多的关系,gorm无法识别外键。
您需要指定将Model.ID
存储在DogData
中的列(gorm默认查找ModelID),
type Model struct {
ID uint `gorm:"primarykey:id" json:"id:_id"`
...
DogData []DogData
}
type DogData struct {
ModelID uint
DogDataID uint
...
}
//CREATE TABLE `dog_data` (`model_id` integer,`dog_data_id` integer,`name` text,`life_span` text,`temperaments` text,CONSTRAINT `fk_models_dog_data` FOREIGN KEY (`model_id`) REFERENCES `models`(`id`))
或者,如果您想使用其他列,请使用foreignKey标签指定
type Model struct {
ID uint `gorm:"primarykey:id" json:"id:_id"`
...
DogData []DogData `gorm:"foreignKey:DogDataID"`
}
type DogData struct {
DogDataID uint
...
}
//CREATE TABLE `dog_data` (`model_id` integer,`dog_data_id` integer,`name` text,`life_span` text,`temperaments` text,CONSTRAINT `fk_models_dog_data` FOREIGN KEY (`dog_data_id`) REFERENCES `models`(`id`))
不确定使用情况,但我认为DogData
可以简化,完全删除Model
结构,因为它与gorm.Model
相同
type DogData struct {
gorm.Model
Name string `json:"name"`
...
}
英文:
> Model's field DogData: define a valid foreign key for relations
Model has one-to-many relation to DogData, for which gorm is not able to identify the foreign key.
You need to specify the column that will store Model.ID
in DogData
(gorm looks for ModelID by default),
type Model struct {
ID uint `gorm:"primarykey:id" json:"id:_id"`
...
DogData []DogData
}
type DogData struct {
ModelID uint
DogDataID uint
...
}
//CREATE TABLE `dog_data` (`model_id` integer,`dog_data_id` integer,`name` text,`life_span` text,`temperaments` text,CONSTRAINT `fk_models_dog_data` FOREIGN KEY (`model_id`) REFERENCES `models`(`id`))
OR if you want to use another column than specify that with foreignKey tag
type Model struct {
ID uint `gorm:"primarykey:id" json:"id:_id"`
...
DogData []DogData `gorm:"foreignKey:DogDataID"`
}
type DogData struct {
DogDataID uint
...
}
//CREATE TABLE `dog_data` (`model_id` integer,`dog_data_id` integer,`name` text,`life_span` text,`temperaments` text,CONSTRAINT `fk_models_dog_data` FOREIGN KEY (`dog_data_id`) REFERENCES `models`(`id`))
Not sure about the use case but I think the DogData
can be simplified as, dropping Model
struct completely as it is same as gorm.Model
type DogData struct {
gorm.Model
Name string `json:"name"`
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论