Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[12.x] fix(postgres): missing parentheses in whereDate/whereTime for json columns #55159

Merged

Conversation

saibotk
Copy link
Contributor

@saibotk saibotk commented Mar 24, 2025

In #12341, the cast to ::date was added to whereDate.

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 the whereTime 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:

select * from "users" where "result"->>'created_at'::date = NOW()

Correct SQL output with this commit:

select * from "users" where ("result"->>'created_at')::date = NOW()

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.

…lumns

In laravel#12341, the cast to `::date`
was added to `whereDate`.

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 the `whereTime` 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:
```SQL
select * from "users" where "result"->>'created_at'::date = NOW()
```

Correct SQL output with this commit:
```SQL
select * from "users" where ("result"->>'created_at')::date = NOW()
```
@saibotk saibotk changed the title fix(postgres): missing parentheses in whereDate/whereTime for json columns [12.x] fix(postgres): missing parentheses in whereDate/whereTime for json columns Mar 24, 2025
@taylorotwell taylorotwell merged commit 386d168 into laravel:12.x Mar 27, 2025
41 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants