fix: [#842] The paginate method will report an error when selecting and renaming only one column#1325
Merged
fix: [#842] The paginate method will report an error when selecting and renaming only one column#1325
Conversation
…nd renaming only one column
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1325 +/- ##
=======================================
Coverage 70.07% 70.07%
=======================================
Files 282 282
Lines 16752 16752
=======================================
Hits 11739 11739
Misses 4522 4522
Partials 491 491 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request fixes an issue where the Paginate method would fail when using a Select clause with column renaming (e.g., Select("name as name")). The fix involves:
- Adding a
resetSelect()method to clear select columns before performing count operations in pagination - Refactoring the
Paginatemethod to avoid building conditions prematurely, allowing the count to properly exclude select columns - Updating the
Selectmethod to append columns instead of replacing them and to remove duplicates - Adding regression tests for the reported issue
Key Changes
- The
Count()method now callsresetSelect()before counting to ensure select columns don't interfere with count queries - The
Select()method now accumulates columns across multiple calls instead of replacing them, making it consistent withdatabase/db/query.go - Duplicate columns are automatically removed using
collect.Unique() - Test queries updated from
Where("name = ?", value)toWhere("name", value)for consistency
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
tests/query_test.go |
Updated test query syntax for consistency and added regression test for issue #842 with select + rename + paginate |
tests/db_test.go |
Added regression test for issue #842 in the DB query builder |
database/gorm/query.go |
Added resetSelect() method, updated Count() to use it, refactored Paginate() to avoid premature condition building, and updated Select() to append columns and remove duplicates |
database/db/query.go |
Updated Select() method to remove duplicate columns and filter out "*" when mixed with other columns |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
devhaozi
approved these changes
Dec 29, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📑 Description
Closes goravel/goravel#842
This pull request addresses issues with the
Selectclause in pagination and counting queries, ensuring that select columns do not interfere with count operations and that duplicate columns are removed. It also adds regression tests to prevent similar issues in the future and refines the test queries for clarity.Database Query Improvements:
resetSelectmethod to the GORMQueryimplementation to clear select columns before performing a count, preventing incorrect results when custom select columns are used in paginated queries. [1] [2]Selectmethod in bothdatabase/db/query.goanddatabase/gorm/query.goto remove duplicate select columns using the newcollect.Uniqueutility. [1] [2]Paginatemethod to avoid reusing query objects that may have lingering select columns, ensuring accurate pagination and counts.Testing and Regression Coverage:
TestPaginatein bothdb_test.goandquery_test.goto verify that custom select columns do not break pagination and counting, specifically addressing The orm paginate method will report an error when query contains Select goravel#842. [1] [2]Dependency Updates:
github.com/urfave/cli/v3as an indirect dependency intests/go.mod.✅ Checks