Skip to content

database/gdb: AllAndCount with multiple fields generates invalid COUNT syntax #4698

@lingcoder

Description

@lingcoder

Go version

go version go1.25.0 linux/amd64

GoFrame version

v2 (master branch, commit sha: latest)

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

Called AllAndCount(true) with multiple fields specified:

result, count, err := db.Model(table).Fields("id, nickname").AllAndCount(true)

Test case reference: Test_Model_AllAndCount_MultipleFields_WithTotal in PR #4703

What did you see happen?

The framework generated invalid COUNT SQL with field list instead of COUNT(*):

SELECT COUNT(id, nickname) FROM `user_xxx`
             ^^ Invalid - COUNT takes 1 argument

SQL execution error:

Error 1064 (42000): You have an error in your SQL syntax ... near ', nickname) FROM ...'

Root cause: When AllAndCount(true) is used with multiple fields, the COUNT query incorrectly includes the field list.

What did you expect to see?

When using AllAndCount(true) with any fields configuration:

-- Data query (correct)
SELECT id, nickname FROM `user_xxx`

-- Count query (should always use COUNT(*) or COUNT(1))
SELECT COUNT(*) FROM `user_xxx`

The count query should always use COUNT(*) or COUNT(1), regardless of the fields specified in the data query.

Additional Context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions