-
Notifications
You must be signed in to change notification settings - Fork 48
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
$modify queries broken in v5.5.1 #102
Comments
Getting the same problem with pg/5.5.1 (caused by the fix for #98). For me, I have a modifier which uses a selectRaw statement (long,lat) to compute a new column (distance), which now errors out with:
If I update the modifier to
You'll get this error: index.js@L436
|
Thanks for the report. I was able to reproduce the issue and added this test: $modify and paginate with no results. I've released v5.5.2 to address this by simply adding a check on the count query result before reading the Let me know if it doesn't solve your issue. |
Thanks for the update @dekelev . Unfortunately I cannot get this to work still - the total is still incorrect. Here's a simplified version of the repro (5.5.2/pg):
returns:
Alright - add the groupBy statement to the modifier as it wants:
Result:
|
I still have the same error as message before (left my code unchanged, didn't add any groupBy clause) |
@alex-all3dp Please share you full example |
@dekelev here is one of the simpler examples that currently fails:
Error:
Model file: class ModelName extends Model {
id: string,
static get modifiers() {
return {
includeRef: (builder: QueryBuilder<ModelName>) =>
builder.withGraphFetched('ref'),
}
}
static get relationMappings() {
return {
ref: {
relation: Model.ManyToManyRelation,
modelClass: 'Ref',
join: {
from: 'ModelName.id',
through: {
from: 'JoinTable.modelNameId',
to: 'JoinTable.refId',
},
to: 'Ref.id',
},
}
} |
Thanks guys! so it seems that the issue is because we now run the ObjectionJS modify method with the count queries and as a result, if |
Hey @dekelev is there any plan to fix this? I actually see two issues here: The first issue: The second issue, which happens when you add the GROUP BY statement, is that the wrong total is returned. That's because COUNT + GROUP BY is counting the number of rows within each group, not the total number of rows returned. One solution here is to put everything into a subquery and count the result of that. An example of what I mean:
This is similar to this issue here: feathersjs-ecosystem/feathers-knex#192 I'm not a DB expert but if this is a valid approach could we expose this as a service option to utilize subqueries for counts? |
@nakedgun The count issue that you're describing is mentioned in the ObjectionJS docs here & here. Regarding the error on fields missing from the groupBy statement - I do plan to check for an automated solution, because I'm not even sure that you can workaround the issue manually using the I do not have much free time these days, so I'm keeping this issue opened until I'll be available for this or will get a relevant PR. |
I've added basic support for modifiers with Can you please check this with your use-cases? Notice that when you use |
@dekelev thank you for your effort and continued support on this. |
@alex-all3dp I can't see any |
@dekelev no I didn't. it would seem odd to me to be required to add a |
There's no requirement to add DISTINCT also acts as GROUP BY, so it's possible that distinct runs somewhere in the lib and is causing this error. The lib runs DISTINCT only when I don't see you used Anyway, I'll try to reproduce the issue on my side based on the examples you shared so far and proceed from there. |
So it seems that COUNT by itself, which is an aggregate function, now requires that non-aggregated columns will not be included in the SELECT. COUNT basically runs GROUP BY under the hood. The issue is that ObjectionJS itself is adding the extra Adding Using You will be able to workaround your issue by using @alex-all3dp @nakedgun Let me know if you have any more suggestions or more use-cases that doesn't work with the strict-group-by branch. If anything goes well, I'll release a new version during next week based on that branch. |
Hey @dekelev . Thanks for your continued work on this! I checked out the strict-group-by branch with my api. Same essential problem though for me, e.g: // model.js
test(builder) {
builder.select(
raw(
'someNumericColumn / 50 as test'
)
).groupBy('id') <-- required, or we error: "GROUP BY clause or be used in an aggregate function at character..."
}, await app.service('offices').find({
query: {
$modify: ['test']
}
})
{
"total": 1, <-- wrong
"limit": 10,
"skip": 0,
"data": [
// correct (omitted)
]
} With this, no error, however In the mean time, I have put in a hack which simulates this kind of developer workflow. I have a hook which detects the use of this modifier in In a perfect world this would all be seamless, but failing that, I guess having some tools to customize the count query whilst making use of feathers-objection internals would be useful, since I suspect there might always be edge cases here. You know the library better than I do though - I'm not sure the best way to expose this, but that's my two cents. Hope it helps! |
I have encountered the same issue and I wasn't willing to give up the cleanliness of the I created a hook called
https://vincit.github.io/objection.js/api/query-builder/other-methods.html#page |
ObjectionJS automatically adds id columns to the The branch I've opened will only detect The service class can also be extended if needed to add any custom workarounds. Changes were released in v5.7 |
@dekelev I still struggle with the fact that a patch release introduces a breaking change that would require all feathers-objection/src/index.js Lines 451 to 453 in 248fe0c
What exactly did this fix? The total count works as expected, even without this line. Removing the block, fixes my issue.Any chance to get this change reverted? |
In this issue, for example, using |
I see. Well, I will just add the required |
@dekelev I now use |
I think 2 workarounds were published in this thread for this count issue.
Make sure that you have upgraded to the latest version of
feathers-objection.
בתאריך יום ו׳, 31 ביולי 2020, 9:02, מאת Alexander Friedl <
[email protected]>:
… @dekelev <https://github.com/dekelev> I no use groupBy('id') in my
modifiers but now encounter the other issue described in this thread, where
the total count is 1.
I tried using eager instead of withGraphFetched as suggested above but
without any luck. Is there a known solution for this issue? From the
answers it seems that there isn't or am I missing something?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#102 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABB5E3L6DCU2E7ZIYIHG4XTR6JM7HANCNFSM4OCBHHMQ>
.
|
@dekelev I looked into these but if you refer to the custom hooks, that just seems overly complicated to me in order to work around a bug. I reverted back to 5.5.0 because it serves my current purpose best. In general I still think the patch to 5.5.1 could have been handled differently, without introducing breaking changes. My suggestion would be to only enable the behaviour in feathers-objection/src/index.js Lines 451 to 453 in 248fe0c
Of course if there were a solution that fixes the issue in a way without breaking other queries that would be preferable to the configuration setting. But as it stands, v5.5.1 and above seem broken to me in regards to handling $modify queries.
|
Just want to add that I still love the library and greatly appreciate all the work an effort you put into it :) |
@dekelev Has there been any resolution on this issue? |
@egluhbegovic @dekelev I just played around with v7.0.0 and the issue still persists unfortunately. |
@dekelev I looked into this a bit more in detail and I think I understand the issue better than before. Would you be open for a PR that introduces a new optional parameter Suggested change: if (query && query.$modify && params.modifierFiltersResults !== false) {
this.modifyQuery(countQuery, query.$modify);
} This check would not break any implementations which rely on the current implementation but would allow to bypass applying the count to the For instance the parameter could be added via a Let me know what you think. Also if you don't have the time but would be open for a PR, I'd be happy to draft one up. Best |
Thanks @alex-all3dp , I will check this out. |
@alex-all3dp, I'm not sure I understand the issue you're having that led you to a solution that prevents running If pagination is not needed, then you can simply set |
@dekelev Well, the So the new flag is mainly a workaround for the issues in objection.js / knex.js that you referenced. It allows to disable the Does that make sense? Unfortunately I can't simply disable pagination,.because it is required for our use case. Looking forward to your feedback! Best |
@alex-all3dp I see your point now, but the tests you've added doesn't seems to reflect your use-case. Can you check them out please? For example, I would expect to see |
@dekelev I was also unhappy this part of the PR, as it as a bit odd to rely on a test that intentionally checks for the incorrect result. All "old" tests are without the |
I'm aware that the test needs to run in MySQL/Postgres in order to make
sense, so you can mention this in the test description and even skip it if
you must, but as long as its description will contain the MySQL/Postgres
term, I would know to run these tests on the relevant DB when something
major is changing.
Regarding the test without the param - once you reproduce your issue in the
tests, I want to make sure that on that specific use-case, no param will
equal to setting the param with true value.
בתאריך שבת, 12 בדצמ׳ 2020, 17:18, מאת Alexander Friedl <
[email protected]>:
… @dekelev <https://github.com/dekelev> I was also unhappy this part of the
PR, as it as a bit odd to rely on a test that intentionally checks for the
incorrect result.
I intended to add a test for exactly the behavior that breaks in our use
case (.withGraphFetched().groupBy('id'). But the issue with the count
breaking for groupBy usage does not occur with SQLite, so the test would
not really assert that the operator fixes the actual issue.
All "old" tests are without the modifierFiltersResults, so it already
shows that nothing breaks, right?
I will look into improving the PR a bit with regards to the test suite.
Thank your for the feedback!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#102 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABB5E3LODVKGGTFCFJRGWBDSUOCT5ANCNFSM4OCBHHMQ>
.
|
@dekelev Makes sense. I updated the PR by using the new |
Thanks @alex-all3dp, it looks great! I'll release it tomorrow. |
Updating to 5.5.1 with objection 2.2.0 and pg 8.2.1 results in the following error message for
GET
requests with$modify
query:"select count(distinct("tableName"."columnName", "tableName"."columnName")) as "total", "tableName"."columnName" from "tableName" - column "tableName.columnName" must appear in the GROUP BY clause or be used in an aggregate function"
the modifier looks something like this:
Does the modifier need to be adapted in any way? It would be odd in my eyes, given that the update is declared as a patch release and should not break existing implementations like this?
The text was updated successfully, but these errors were encountered: