users使用gorm,我為和product_prices表創(chuàng)建了以下模型// model for table `users`type User struct { Id uint64 `json:"id" gorm:"primaryKey"` Email string `json:"email" gorm:"unique"` Password []byte `json:"-"` CreatedOn time.Time `json:"created_on" gorm:"<-:create"` UpdatedOn time.Time `json:"updated_on"`}// model for table `product_prices`type ProductPrice struct { Id uint64 `json:"id" gorm:"primaryKey"` Price float64 `json:"price"` Direction string `json:"direction"` NotificationMethod string `json:"notification_method"` CreatedOn time.Time `json:"created_on,omitempty" gorm:"<-:create"` UpdatedOn time.Time `json:"updated_on,omitempty"` LastNotified time.Time `json:"last_notified,omitempty"` UserId uint64 `json:"user_id"` User User `json:"-" gorm:"foreignKey:UserId"`}我想做一些類(lèi)似在product_prices和users表之間執(zhí)行以下sql連接的事情select product_prices.id, product_prices.price, product_prices.created_on, users.email as user_email, users.created_on as user_created_on from product_prices join users on product_prices.user_id=users.id;這是我嘗試過(guò)但仍然不走運(yùn)的許多事情之一,這要?dú)w功于文檔頁(yè)面https://gorm.io/docs/query.html#Joins不夠清晰, 我還想遍歷行按照這里的文檔https://gorm.io/docs/query.html#Joins我嘗試了以下// struct for result of join querytype ProductPriceUser struct { Id uint64 `json:"id"` Price float64 `json:"price"` CreatedOn time.Time `json:"created_on,omitempty"` UserEmail User `json:"user_email"` UserCreatedOn User `json:"user_created_on"`}但是我遇到了各種各樣的錯(cuò)誤,我做錯(cuò)了什么,我如何得到我想要的,如上所述?謝謝!
如何使用 GORM 執(zhí)行聯(lián)接并遍歷結(jié)果行?
翻翻過(guò)去那場(chǎng)雪
2022-10-31 15:55:33