Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FieldError: Invalid field name(s) given in select_related: 'userinfo'. Choices are: userinfo

I get this error when trying to use only with select_related.

FieldError: Invalid field name(s) given in select_related: 'userinfo'. Choices are: userinfo

It's a little strange that it reports the field I'm trying to select as an error. Here is my query:

users_with_schools = User.objects.select_related('userinfo').only(
    "id",
    "date_joined",
    "userinfo__last_coordinates_id",
    "userinfo__school_id"
).filter(
    userinfo__school_id__isnull=False,
    date_joined__gte=start_date
)

I've been able to use select_related with only in other places in my code so I am not sure why this is happening.

Edit: Here is the full traceback

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "env/lib/python2.7/site-packages/django/db/models/query.py", line 138, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "env/lib/python2.7/site-packages/django/db/models/query.py", line 162, in __iter__
    self._fetch_all()
  File "env/lib/python2.7/site-packages/django/db/models/query.py", line 965, in _fetch_all
    self._result_cache = list(self.iterator())
  File "env/lib/python2.7/site-packages/django/db/models/query.py", line 238, in iterator
    results = compiler.execute_sql()
  File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 818, in execute_sql
    sql, params = self.as_sql()
  File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 367, in as_sql
    extra_select, order_by, group_by = self.pre_sql_setup()
  File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 48, in pre_sql_setup
    self.setup_query()
  File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 39, in setup_query
    self.select, self.klass_info, self.annotation_col_map = self.get_select()
  File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 203, in get_select
    related_klass_infos = self.get_related_selections(select)
  File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 743, in get_related_selections
    ', '.join(_get_field_choices()) or '(none)',
FieldError: Invalid field name(s) given in select_related: 'userinfo'. Choices are: userinfo
like image 836
Eric Conner Avatar asked Mar 13 '23 07:03

Eric Conner


1 Answers

From the documentation:

All of the cautions in the note for the defer() documentation apply to only() as well. Use it cautiously and only after exhausting your other options.

...

Using only() and omitting a field requested using select_related() is an error as well.

select_related will try to fetch all the columns for userinfo. As described above, trying to restrict the set of columns to specific ones will result in an error - the combination of select_related and only does not support that.

It's probably worth noting the comment that goes with these methods:

The defer() method (and its cousin, only(), below) are only for advanced use-cases. They provide an optimization for when you have analyzed your queries closely and understand exactly what information you need and have measured that the difference between returning the fields you need and the full set of fields for the model will be significant.

Edit: you mention in a comment below that the following query, used elsewhere in your code, seems to work fine:

ChatUser.objects.select_related("user__userinfo").\
    only( "id", "chat_id", "user__id", "user__username", "user__userinfo__id" )

My best guess is that you are hitting this bug in Django (fixed in 1.10).

I suppose the easiest way to verify is to check the SQL query generated by the queryset that seems to work. My guess is that you will find that it isn't actually querying everything in one go and that there are additional queries when you try to access the related model that you asked to be fetched in select_related.

like image 198
solarissmoke Avatar answered Apr 27 '23 11:04

solarissmoke