[英]golang, creating relation between 2 model and retrieve them with Preload using gorm
I am learning golang with gqlgen and gorm as orm, I am creating an app using with 2 models user and messages where the user has a list of messages, and the messages have sender and recipient.我正在使用 gqlgen 和 gorm 作为 orm 学习 golang,我正在使用 2 个模型用户和消息创建一个应用程序,其中用户有一个消息列表,并且消息有发送者和接收者。 I have made them like the following
我使它们如下所示
type User struct {
ID string `json:"id" gorm:"primary_key;type:uuid;default:uuid_generate_v4()"`
Username string `json:"username"`
Email string `json:"email"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Messages []*Message `json:"messages"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at" sql:"index"`
}
type Message struct {
ID string `json:"id" gorm:"primary_key;type:serial"`
Title string `json:"title"`
Body string `json:"body"`
DueDate time.Time `json:"dueDate"`
IsViewed bool `json:"isViewed" gorm:"default:false"`
SenderID string `json:"senderId" gorm:"type:uuid"`
Sender *User `json:"sender" gorm:"foreignkey:SenderID"`
RecipientID string `json:"recipientId" gorm:"type:uuid"`
Recipient *User `json:"recipient" gorm:"foreignkey:RecipientID"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at" sql:"index"`
}
when I retrieve the messages data using Preload当我使用 Preload 检索消息数据时
var messages []*models.Message
err := db.
Preload("Sender").
Preload("Recipient").
Find(&messages).Error
if err != nil {
return nil, err
}
return messages, err
it works perfectly but my problem is when trying to retrieve the user with the messages preloaded.它工作得很好,但我的问题是在尝试检索预加载消息的用户时。
var users []*models.User
err := db.
Preload("Messages").
Find(&users).Error
if err != nil {
return nil, err
}
return users, err
this one gives me the following error can't preload field Messages for models.User
这给了我以下错误
can't preload field Messages for models.User
I know I might design my schema wrong if there's a better way to organize it I would appreciate it so much, thanks in advance.我知道如果有更好的组织方式,我可能会错误地设计我的架构,我将非常感激,在此先感谢。
I think you should separate messages in User
to SentMessages
and ReceivedMessages
.我认为您应该将
User
消息与SentMessages
和ReceivedMessages
分开。 Then you can specify foreign keys in User
like that:然后你可以像这样在
User
指定外键:
SentMessages []*Message `gorm:"foreignkey:SenderID" json:"sentMessages"`
ReceivedMessages []*Message `gorm:"foreignkey:RecipientID" json:"receivedMessages"`
then use it as the following:然后将其用作以下内容:
var users []*models.User
err := db.
Preload("SentMessages").
Preload("ReceivedMessages").
Find(&users).Error
if err != nil {
return nil, err
}
return users, err
that should work as you want这应该可以正常工作
I think you've to take a closer look on how the association are made(actually create from the official gorm package) like (&Model{}).Create(target).Association("Column").Append(&related_instance)
我认为您必须仔细查看关联的创建方式(实际上是从官方 gorm 包创建),例如
(&Model{}).Create(target).Association("Column").Append(&related_instance)
So when you'll retry it using Preload everything gone be Ok.因此,当您使用 Preload 重试时,一切正常。 And BTW it work with your current design.
顺便说一句,它适用于您当前的设计。 Hope it helps.
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.