[5.8] Fix columns parameter on paginate method#28937
Merged
taylorotwell merged 1 commit intolaravel:5.8from Jun 24, 2019
driesvints:fix-columns-param-on-paginate-method
Merged
[5.8] Fix columns parameter on paginate method#28937taylorotwell merged 1 commit intolaravel:5.8from driesvints:fix-columns-param-on-paginate-method
taylorotwell merged 1 commit intolaravel:5.8from
driesvints:fix-columns-param-on-paginate-method
Conversation
This is currently broken and will throw a SQL exception:
DB::table('posts')->paginate(5, ['title', 'content']);
At the moment when you attempt to pass specific columns on the paginate method on the base query builder it will fail when you provide more than one column. SQL queries can only handle a count for a specific column and not multiple ones. That's why it's unwanted to pass along the columns parameter from the paginate method to the getCountForPagination method. Since we just want to do a count of the current result set a simple count with the '*' sign is enough. At the moment the Eloquent builder already handles pagination in this way.
It's noted by Staudenmeier that the only difference is when you provide a specific column to the getCountForPagination method is that it'll only count non-NULLs. However, it's not feasible to allow this to be done in combination with the paginate method because the columns param on the paginate method soley exists to filter which columns will be retrieved in the paginated result set (which is currently broken thus). See #28844 (comment)
I've added an extra integration test which reproduces the problem at hand. I've updated the tests in the DatabaseQueryBuilderTest class to reflect the changes made to getCountForPagination call.
This has been a long outstanding issue so hopefully this will prevent more issues from popping up.
Fixes #28890
Member
Author
|
@staudenmeir could you perhaps give this PR a second look when you find some time? Thanks! :) |
This was referenced Jun 24, 2019
staudenmeir
approved these changes
Jun 24, 2019
|
Not a SQL genius, but just an FYI for future Googlers: If your query relies on SELECT DISTINCT, i.e., if you use |
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.
This is currently broken and will throw a SQL exception:
At the moment when you attempt to pass specific columns on the paginate method on the base query builder it will fail when you provide more than one column. SQL queries can only handle a count for a specific column and not multiple ones. That's why it's unwanted to pass along the columns parameter from the paginate method to the getCountForPagination method. Since we just want to do a count of the current result set a simple count with the '*' sign is enough. At the moment the Eloquent builder already handles pagination in this way.
It's noted by @staudenmeir that the only difference is when you provide a specific column to the getCountForPagination method is that it'll only count non-NULLs. However, it's not feasible to allow this to be done in combination with the paginate method because the columns param on the paginate method soley exists to filter which columns will be retrieved in the paginated result set (which is currently broken thus). See #28844 (comment)
I've added an extra integration test which reproduces the problem at hand. I've updated the tests in the DatabaseQueryBuilderTest class to reflect the changes made to getCountForPagination call.
This has been a long outstanding issue so hopefully this will prevent more issues from popping up.
Fixes #28890