I use golang"s gorm to construct a data model. One posts, one users, one user corresponds to multiple articles
posts.go
finally this problem arises, how to solve it
I use golang"s gorm to construct a data model. One posts, one users, one user corresponds to multiple articles
posts.go
finally this problem arises, how to solve it
your relationship is actually a User to which multiple Posts belong.
so it should be
The key location of thePost table is
type Post struct {
//
UserID int `gorm:"index" //
User User
}
query directly
db.Where("user_id = ?", userId).Preload("User").Find(&posts)
Previous: Multiple changes of table about this.$refs in ElementUI
Next: Please tell me how to follow the mouse to move relative to the parent box.
how does gorm create data for json fields? the official document has an example of creating a record: http: gorm.io zh_CN docs ind. db.Create(&Product{Code: "L1212", Price: 1000}) question: if this Product model has a specifi...
package main import ( "database sql driver" "fmt" "time" ) type JSONTime struct { time.Time } MarshalJSON on JSONTime format Time field with %Y-%m-%d %H:%M:%S func (t JSONTime) MarshalJSON() ([]byte, er...