type Message struct {
gorm.Model
Attachment Attachment
}
type Attachment struct {
gorm.Model
MessageID uint
}
Now, if you try this:
db.Model(&models.Message{}).Where(&models.Message{
Model: gorm.Model{
ID: 1,
},
}).Preload("Attachment")).First(&message)
All works perfectly, and Attachment is preloaded
But if you try this:
db.Model(&models.Message{
Model: gorm.Model{
ID: body.MessageID,
},
}).Preload("Attachment").First(&message)
Message will finded, but attachment will not preload. Is it a bug or feature?
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: twocs
It's hard to tell because you forgot to add a Playground reproducible, but I think you are pointing out a difference in behaviour between the docs and ID in gorm.Model. The docs say:
When the destination object has a primary value, the primary key will be used to build the condition, for example:
var user = User{ID: 10}
db.First(&user)
// SELECT * FROM users WHERE id = 10;
var result User
db.Model(User{ID: 10}).First(&result)
// SELECT * FROM users WHERE id = 10 AND deleted_at IS NULL;
However, when we use gorm.Model, the ID does not get used. Does that mean it's not a "primary value"?
var user = User{Model: gorm.Model{ID: 10}}
db.First(&user)
// SELECT * FROM `users` WHERE `users`.`id` = 10 AND `users.``deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1;
var result User
db.Model(Model: gorm.Model{ID: 10}).First(&result)
// SELECT * FROM `users` WHERE `users.``deleted_at` IS NULL ORDER BY `users`.`id` LIMIT 1;
Personally I would discourage modifying the object in the Model func, because it might have unexpected results. For example, if you do an updates but have already preloaded other fields, gorm might like to update your preload tables.
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨