Replies: 1 comment
-
At least type User struct {
gorm.Model
Username string
- Orders []Order
+ Orders []Order `gorm:"foreignKey:UserID;references:ID"`
}
type CustomUserRes struct {
+ ID uint `gorm:"primaryKey"`
Username string // <--- No gorm default fields required
- Orders []Order
+ Orders []Order `gorm:"foreignKey:UserID;references:ID"`
}
var user User
var userCustom CustomUserRes
var ordersCustom CustomOrderRes
db.Preload("Orders", func (tx *gorm.DB) *gorm.DB {
- return tx.Scan(&ordersCustom) // <--- I want to fit the preload result to ordersCustom
+ return tx.Model(&Order{}).Scan(&ordersCustom) // <--- I want to fit the preload result to ordersCustom
}).Model(&user).First(&userCustom) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Having the follow example:
How can I query a
user
with all its Orders but using the custom model ofCustomOrderRes
in the preload.I tried something like but didn't work:
Beta Was this translation helpful? Give feedback.
All reactions