Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

FieldError when using select_related with enable_only_optimization #152

Open
blueyed opened this issue Dec 16, 2022 · 5 comments
Open

FieldError when using select_related with enable_only_optimization #152

blueyed opened this issue Dec 16, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@blueyed
Copy link
Contributor

blueyed commented Dec 16, 2022

When trying to use select_related on a field, I am getting an error when the DjangoOptimizerExtension is used with its default enable_only_optimization=True.

This happens because only() is used with "id", and then select_related is issued on top:

if config.enable_select_related and self.select_related:
qs = qs.select_related(*self.select_related)
if config.enable_only and self.only:
qs = qs.only(*self.only)

django.core.exceptions.FieldError: Field Foo.bar cannot be both deferred and traversed using select_related at the same time.

A workaround is to add the field also to only, but that could be done automatically then also.

@django_type(models.Foo, filters=FooFilter, pagination=True)
class Foo:
    bar: Bar
    special_bar: Bar | None = gql.django.field(
        only=["bar"],  # workaround
        select_related=["bar"]
    )

I am not sure about the select_related being necessary/useful here yet (maybe because of the Optional/None it might be necessary), but I think it's a generic issue.

I also wondered what's the point of enable_only_optimization. Is it to reduce the amount of data being selected/transferred only?

(related)

@blueyed
Copy link
Contributor Author

blueyed commented Dec 16, 2022

This is related to #154, i.e. it is with a non-model field, and specifying field_name fixes it / helps there.

@bellini666
Copy link
Member

Is that special_bar a property?

And yes, the only optimization is to reduce the amount of data being selected and transferred. It does make some difference in speed, but it is the least important optimization indeed.

@blueyed
Copy link
Contributor Author

blueyed commented Dec 20, 2022

Is that special_bar a property?

Yes.
It's to a FK's property, which I am turning into an actual field, but field_name="bar__special" is not supported (no splitting by field separators).. but here select_related (or rather model properties as you mentioned in #154 (comment) should work better then).

@bellini666
Copy link
Member

Should I close this issue then since the model property solves the issue? Or is there something extra that we should support here?

@bellini666 bellini666 added the enhancement New feature or request label Dec 21, 2022
@blueyed
Copy link
Contributor Author

blueyed commented Mar 6, 2023

I think having to specify only as a workaround should/could be handled automatically.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants