Gorm one-to-many mapping problem

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

Feb.15,2022

your relationship is actually a User to which multiple Posts belong.

so it should be

The key location of the

Post table is

type Post struct {
    // 
    UserID int `gorm:"index" //
    User    User    
}

query directly

db.Where("user_id = ?", userId).Preload("User").Find(&posts)
  • How does gorm create data for json fields?

    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...

    Dec.20,2021
  • Gorm query problem

    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...

    Jan.08,2022
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b38687-2c0f1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b38687-2c0f1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?