mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
612e545ae29a3dc1d7cbdc3b22628a00f5346a45
useFieldForCount is true in AllAndCount/ScanAndCount (#4701)
## Summary
Fix bug where `AllAndCount(true)` with multiple fields generates invalid
SQL `COUNT(field1, field2, ...)` causing syntax error.
## Root Cause
When `useFieldForCount=true`, the COUNT query inherits the fields
configuration from the model:
```go
// Before (buggy code)
if !useFieldForCount {
countModel.fields = []any{Raw("1")}
}
// When useFieldForCount=true, fields remain as ["id", "nickname"]
// Generates: SELECT COUNT(id, nickname) FROM table ❌
```
## Fix
Always use `COUNT(1)` regardless of `useFieldForCount` parameter since
COUNT() accepts only one argument:
```go
// After (fixed code)
// Always use COUNT(1) for counting, regardless of useFieldForCount.
// COUNT() accepts only one argument, so we can't use multiple fields.
countModel.fields = []any{Raw("1")}
```
Applied to both `AllAndCount()` and `ScanAndCount()` methods.
## Tests
Added `Test_Issue4698` with 5 test cases:
1. AllAndCount(true) with multiple fields
2. AllAndCount(false) with multiple fields (baseline)
3. ScanAndCount with multiple fields
4. AllAndCount with single field
5. AllAndCount with WHERE condition
All tests verify that COUNT generates valid SQL and returns correct
count.
## Related
Fixes #4698
Ref #4703 (discovered during pagination test development)
refactor(container): add default nil checker, rename RegisterNilChecker to SetNilChecker, migrate instance containers to type-safe generics (#4630)
fix(databse/gdb): use COUNT(1) if fields number is greater than 1 even when parameter
useFieldForCount is true in AllAndCount/ScanAndCount (#4701)
fix(databse/gdb): use COUNT(1) if fields number is greater than 1 even when parameter
useFieldForCount is true in AllAndCount/ScanAndCount (#4701)
refract(gerror): add ITextArgs interface and its implements, mainly for i18n that needs text and args separately (#4597)
English | 简体中文
A powerful framework for faster, easier, and more efficient project development.
Installation
go get -u github.com/gogf/gf/v2
Documentation
- Official Site: https://goframe.org
- Official Site(en): https://goframe.org/en
- 国内镜像: https://goframe.org.cn
- Mirror Site: https://pages.goframe.org
- Mirror Site: Offline Docs
- GoDoc API: https://pkg.go.dev/github.com/gogf/gf/v2
- Doc Source: https://github.com/gogf/gf-site
Contributors
💖 Thanks to all the contributors who made GoFrame possible 💖
License
GoFrame is licensed under the MIT License, 100% free and open-source, forever.
Languages
GO
100%
