You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you add a Joins() preload statement to a *gorm.DB transaction instance, the select statements to loads field from the relationship seems to be nullified if you first call tx.Count() and then afterwards with the same tx also call tx.Find(). The tx.Find() no longer selects the fields from the joined tables, e.g.
varres []MyModeltx:=db.Model(&MyModel)
tx.Joins("OtherModel")
varcnt*int64tx.Count(&cnt)
tx.Find(&res)
// res.OtherModel.<somefield> is empty as SELECT for OtherModels have disappeared from the query
A use case for this scenario is if you are paginating: You first want to get the total records in the DB to show in the UI (or calculate the amount of pages) and after doing so, you apply a limit and offset to the tx to obtain a subset of the results.
If you call the join statements again, this results in an error:
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 ✨
dschreij
changed the title
Count() erases earlier Joins() statement
Count() erases SELECTs for Joins() statement if executed before a Find()
Jan 10, 2024
GORM Playground Link
go-gorm/playground#681
Description
If you add a Joins() preload statement to a *gorm.DB transaction instance, the select statements to loads field from the relationship seems to be nullified if you first call
tx.Count()
and then afterwards with the same tx also calltx.Find()
. Thetx.Find()
no longer selects the fields from the joined tables, e.g.A use case for this scenario is if you are paginating: You first want to get the total records in the DB to show in the UI (or calculate the amount of pages) and after doing so, you apply a limit and offset to the tx to obtain a subset of the results.
If you call the join statements again, this results in an error:
pq: table name \"OtherModel\" specified more than once
The text was updated successfully, but these errors were encountered: