Your Question
I've read the document and watched such video, there still a question that I wanna to figure out; There's struct include more than 10 fields and struct, when the POST data send and I'm not sure how many fields the user passed in, how can I query those fields in database?
Expected answer
How many fields the user passed in and how many fields i query from database, How do you implement this logic with gorm? Looking forward to reply.
Comment From: a631807682
When querying with struct, GORM will only query with non-zero fields https://gorm.io/docs/query.html#Struct-amp-Map-Conditions https://gorm.io/docs/query.html#Specify-Struct-search-fields
Comment From: infoBrainSys
type User struct {
*gorm.Model
Name string `form:"name"`
Password string `form:"password"`
Orders []Order `gorm:"foreignKey:UserID"` // has many
}
type Order struct {
*gorm.Model
Number string `form:"number"`
UserID uint
}
var result User
db.Preload("Orders").Model(&query).Find(&result)
query is a parameter structure passed by the user through a POST request create by Gin.ShouldBind(), while I was running this demo, All data in the database will be queried in result, regardless of whether I pass any parameter or if it is null. Could you give me a better suggestion to solve this problem?
Comment From: a631807682
https://gorm.io/docs/performance.html#Select-Fields