Invalid fields of structure found during automatic migration
Question content
When I try to automatically migrate the table according to my structure, I get this error, I don't know why I get 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:, 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
I am new to golang language and gorm, especially when it comes to creating structures for raw json data, here is my intention of using gorm's structure:
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"` }
Solution
Model’s fields dogdata: Define valid foreign keys for relationships
The model has a one-to-many relationship with dogdata, and gorm cannot recognize foreign keys.
You need to specify the column to 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 instead of specifying the column using 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 dogdata
can be simplified to, removing the model
structure entirely since it is the same as gorm.model
type DogData struct { gorm.Model Name string `json:"name"` ... }
The above is the detailed content of Invalid fields of structure found during automatic migration. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...
