Skip to content

fix(database/gdb): Raw SQL Count ignores Where condition#4611

Merged
hailaz merged 3 commits intogogf:masterfrom
lingcoder:hotfix/issue4500
Jan 16, 2026
Merged

fix(database/gdb): Raw SQL Count ignores Where condition#4611
hailaz merged 3 commits intogogf:masterfrom
lingcoder:hotfix/issue4500

Conversation

@lingcoder
Copy link
Contributor

Summary

  • Fixed a bug where Raw() with Where() and Count()/ScanAndCount() was ignoring the Where conditions in Count queries
  • The issue was in getFormattedSqlAndArgs() which returned nil for conditionArgs without calling formatCondition() for Raw SQL in SelectTypeCount case

Changes

  • Modified database/gdb/gdb_model_select.go to call formatCondition() for Raw SQL Count queries
  • Added comprehensive test cases for MySQL and PostgreSQL drivers
  • Fixed incorrect test expectation in Test_Model_Raw

Test plan

  • Added Test_Issue4500 with 6 edge cases covering:
    • Raw SQL with WHERE + external Where condition
    • Raw SQL without WHERE + external Where condition
    • Raw + Where + ScanAndCount
    • Raw + multiple Where conditions
    • Raw SQL with no external Where (baseline)
    • Verify All() still works correctly
  • All tests pass on PostgreSQL

Closes #4500

When using Raw() with Where() and Count()/ScanAndCount(), the Where
conditions were being ignored in the Count query. This was because
getFormattedSqlAndArgs() for SelectTypeCount with Raw SQL was returning
nil for conditionArgs without calling formatCondition().

The fix adds formatCondition() call for Raw SQL in SelectTypeCount case,
ensuring Where conditions are properly included in the subquery.

Closes gogf#4500
@hailaz hailaz merged commit 5d1712b into gogf:master Jan 16, 2026
18 checks passed
@lingcoder lingcoder deleted the hotfix/issue4500 branch February 28, 2026 00:38
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.

orm: Model 的 Raw() 的 Count 忽略了Where 条件

2 participants