在自动迁移期间发现了结构体的无效字段。

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

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:&quot;primarykey:id&quot; json:&quot;id:_id&quot;`
	CreatedAt time.Time  `gorm:&quot;column:created_at&quot; json:&quot;created_at&quot;`
	UpdatedAt time.Time  `gorm:&quot;column:updated_at&quot; json:&quot;updated_at&quot;`
	DeletedAt *time.Time `gorm:&quot;column:deleted_at&quot; json:&quot;deleted_at&quot;`
	DogData   []DogData
}

type DogData struct {
	DogDataID    uint
	Name         string         `json:&quot;name&quot;`
	Life_Span    string         `json:&quot;life_span&quot;`
	Temperaments string         `json:&quot;temperament&quot;`
	Weight       datatypes.JSON `json:&quot;weight&quot;`
	Height       datatypes.JSON `json:&quot;height&quot;`
	Image        datatypes.JSON `json:&quot;image&quot;`
}

type Weight struct {
	Imperial string `json:&quot;imperial&quot;`
	Metric   string `json:&quot;metric&quot;`
}

type Height struct {
	Imperial string `json:&quot;imperial&quot;`
	Metric   string `json:&quot;metric&quot;`
}

type Image struct {
	URL string `json:&quot;url&quot;`
}

答案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:&quot;primarykey:id&quot; json:&quot;id:_id&quot;`
...
	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:&quot;primarykey:id&quot; json:&quot;id:_id&quot;`
...
	DogData   []DogData  `gorm:&quot;foreignKey:DogDataID&quot;`

}

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:&quot;name&quot;`
    ...
}

huangapple
  • 本文由 发表于 2023年6月14日 07:55:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76469362.html
匿名

发表评论

匿名网友

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

确定