Skip to content

fix(query-builder): default alias for count('*') is no longer invalid SQL#14

Merged
alexstandiford merged 1 commit into
mainfrom
fix/count-star-default-alias
May 26, 2026
Merged

fix(query-builder): default alias for count('*') is no longer invalid SQL#14
alexstandiford merged 1 commit into
mainfrom
fix/count-star-default-alias

Conversation

@alexstandiford

Copy link
Copy Markdown
Contributor

Summary

`count('*')` without an explicit alias produces `as *_count`, which isn't a valid SQL identifier:

```
SELECT COUNT(*) as _count FROM users AS u
^^^^^^^^
syntax error near "
"
```

This defaults to the bare identifier `count` when the field is `*`. Existing behavior for non-star fields (`field_count`, with the field name as prefix) is preserved.

Why this matters

Caught while porting the QueryBuilder to `phpnomad/sqlite-integration`. A test that called `->count('*')` without an alias produced a query that the engine rejected. The SQLite port carries the same fix.

Test plan

  • Existing test suite continues to pass
  • A new test in this repo: `->count('')->build()` produces SQL whose alias parses cleanly (e.g. assert the emitted alias is `count`, not `_count`)
  • Existing usages that pass an explicit alias remain unchanged

… SQL

`count('*')` without an explicit alias produced `as *_count`, which isn't
a valid SQL identifier:

    SELECT COUNT(*) as *_count FROM users AS u
                       ^^^^^^^^
                       syntax error near "*"

Default to the bare identifier `count` when the field is `*`; existing
behavior for non-star fields is preserved.

Caught while porting the QueryBuilder to phpnomad/sqlite-integration —
the SQLite port carries the same fix.
@alexstandiford alexstandiford force-pushed the fix/count-star-default-alias branch from acab601 to 275a14a Compare May 26, 2026 18:01
@alexstandiford alexstandiford merged commit 3060b0e into main May 26, 2026
8 checks passed
@alexstandiford alexstandiford deleted the fix/count-star-default-alias branch May 26, 2026 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant