You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here, I have a custom property on my model that gets the main identifier from all the identifiers, so I want to do a prefetch here to avoid n + 1 issues.
The following query will now cause issues, though:
Calling this query once works fine, however, the second request will lead to this error:
Cannot find 'my_parent' on MyModel object, 'my_parent_model__my_model__my_parent_model__my_model__identifiers' is an invalid parameter to prefetch_related()
Further subsequent requests each add a my_parent_model__my_model__ to the beginning of the invalid prefetch.
My debugging lead me to the conclusion, that the culprit is line 444 in strawberry-django/optimizer.py : store |= f_store.with_prefix(path, info=info)
In f_store.with_prefix the actual Prefetch object will prepend the path. This is however a by reference pass of the actual Prefetch object of the field, as this is never copied along the way. This way, after the first call, the prefetch stacks up the prefixes and pollutes/corrupts the field for subsequent calls.
System Information
Operating system: Mac OS
Strawberry version (if applicable):
strawberry-graphql==0.209.0
strawberry-graphql-django==0.17.0
Additional Context
My suggestion would be to have a copy of the prefetch object / the field's store when getting it in line 400 in optimizer.py. This would avoid downstream issues.
If you agree with this approach, I can also attempt fixing it as my first issue.
Upvote & Fund
We're using Polar.sh so you can upvote and help fund this issue.
We receive the funding once the issue is completed & confirmed by you.
Thank you in advance for helping prioritize & fund our backlog.
The text was updated successfully, but these errors were encountered:
Describe the Bug
When using the DjangoOptimizer extension and adding prefetching to type's field, the field's store gets corrupted on each query.
Example:
Here, I have a custom property on my model that gets the main identifier from all the identifiers, so I want to do a prefetch here to avoid n + 1 issues.
The following query will now cause issues, though:
Calling this query once works fine, however, the second request will lead to this error:
Cannot find 'my_parent' on MyModel object, 'my_parent_model__my_model__my_parent_model__my_model__identifiers' is an invalid parameter to prefetch_related()
Further subsequent requests each add a
my_parent_model__my_model__
to the beginning of the invalid prefetch.My debugging lead me to the conclusion, that the culprit is line 444 in strawberry-django/optimizer.py :
store |= f_store.with_prefix(path, info=info)
In
f_store.with_prefix
the actual Prefetch object will prepend the path. This is however a by reference pass of the actual Prefetch object of the field, as this is never copied along the way. This way, after the first call, the prefetch stacks up the prefixes and pollutes/corrupts the field for subsequent calls.System Information
Additional Context
My suggestion would be to have a copy of the prefetch object / the field's store when getting it in line 400 in optimizer.py. This would avoid downstream issues.
If you agree with this approach, I can also attempt fixing it as my first issue.
Upvote & Fund
The text was updated successfully, but these errors were encountered: