[12.x] fix(postgres): missing parentheses in whereDate/whereTime for json columns #55159
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.
In #12341, the cast to
::date
was added towhereDate
.This leads to an error with PostgreSQL when passing in json column selectors. For example:
->whereDate('result->created_at', DB::raw('NOW()'))
will throw a syntax error for type date. The same is true for thewhereTime
function with its::time
cast.To fix this, we need to wrap the column identifier in parentheses, when they are json paths.
Current SQL output:
Correct SQL output with this commit:
I opted to only add the parentheses when the column is a json path, as this is the only case where the cast is necessary.
This is at the cost of another
str_contains
check. Which should be fine but we could also opt to just always add the parentheses if you want to.Note: I checked all other functions and these two should be the only ones that are affected by this. The
whereBasic
also does cast to::text
but PostgreSQL did not complain on text casts.