I queried how many rows in tables use DB.Count(), it return 0, but there are 1 row matched in table. I trace this problem, and find the source code of DB.Count in 'finisher_api.go'.
`func (db DB) Count(count int64) (tx *DB) { tx = db.getInstance() ...
tx.Statement.Dest = count
tx = tx.callbacks.Query().Execute(tx)
if tx.RowsAffected != 1 {
*count = tx.RowsAffected
}
return
}`
Why use tx.RowsAffected != 1 to decide if set RowsAffected value to *count ? I am confused with it, and did not got it. I think it should be
if tx.RowsAffected != 0 {
*count = tx.RowsAffected
}
So, anyone can help me to explain it's reason ?
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 2 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: ghost
https://gorm.io/zh_CN/docs/advanced_query.html#Count 这里 count 的意思是 Get matched records count
情况如下:
- count() 且没有 group by 这里只会返回 一行 数据,记录数是 count()的结果
- count() 有 group by ,这会返回 多行 数据,这里选择使用 RowsAffected
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 2 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: choover-broad
We ran into this today, oof. It seems to only happen when exactly one row is matched.