-
-
Notifications
You must be signed in to change notification settings - Fork 35
Fix Syntax Error in CAST Statement for Postgres Attachment Count Query #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Thanks for the contribution @thienvu18! Any chance you're able to add a test case for this? Also, is this an issue that's present in Laravel as well? |
|
Thank for your quick reply. Sorry that I am not good at writing test cases. However, we can produce this bug using our Test Plugin as follows:
portfolio_count:
label: Portfolio count
relation: portfolio
useRelationCount: true
Then you will get an exception like this Notice at the generated sql statement, you will see an incorrect placement of quotes in the CAST statement This bug only happens when using postgres database because |
|
Tested and confirmed the issue. This is caused by I had a look through the code base and cannot find any other instances of It may be benefitial to rely on Maybe something like: $query = $query->select($columns)->whereRaw(sprintf(
'%s = %s',
$query->getGrammar()->wrap($this->getExistenceCompareKey()),
DbDongle::cast($query->getGrammar()->wrap($this->getQualifiedParentKeyName()), 'TEXT')
)); |
jaxwilko
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
@thienvu18 can you test out the latest changes that we've made and let me know how it goes for you? Then we should be able to merge it. |
|
@LukeTowers Thanks for all your hard work on this – much appreciated! Let me know if you need anything else. Cheers! |

When querying the Postgres database, the attachment count query was generated as follows:
select count(*) from "system_files" where "system_files"."attachment_id" = "CAST(some_table"."id" as "TEXT)" and "system_files"."attachment_type" = Some\Model and "field" = images) as "images_count"This query results in a syntax error due to an incorrect placement of double quotes in the CAST statement.
Changes Made:
Fixed the syntax error by properly wrapping keys before casting.
Used raw queries to prevent issues with double wrapping of keys.