#24612: Confusing error message when using only/defer through deleted related
field
----------------------------------------------+--------------------
Reporter: Naddiseo | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Database layer (models, ORM) | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
If one tries to use `defer`/`only` for a field in a model that has the
reverse relationship explicitly removed, the resulting error message is
confusing.
{{{
#!python
# models.py
from django.db import models
from django.contrib.auth.models import User
class Message(models.Model):
content = models.CharField(max_length=10)
class MessageParticipant(models.Model):
msg = models.ForeignKey(Message, related_name='participants')
user = models.ForeignKey(User, related_name='+')
}}}
Query to reproduce:
{{{
list(Message.objects.all().only('participants__user__username')
}}}
Error:
{{{
Traceback (most recent call last):
File "/usr/lib/python3.4/unittest/loader.py", line 312, in _find_tests
module = self._get_module_from_name(name)
File "/usr/lib/python3.4/unittest/loader.py", line 290, in
_get_module_from_name
__import__(name)
File "~/tmp/sr/test.py", line 11, in <module>
print(q.query)
File "~/.virtualenvs/dj18_sr/lib/python3.4/site-
packages/django/db/models/sql/query.py", line 213, in __str__
sql, params = self.sql_with_params()
File "~/.virtualenvs/dj18_sr/lib/python3.4/site-
packages/django/db/models/sql/query.py", line 221, in sql_with_params
return self.get_compiler(DEFAULT_DB_ALIAS).as_sql()
File "~/.virtualenvs/dj18_sr/lib/python3.4/site-
packages/django/db/models/sql/compiler.py", line 367, in as_sql
extra_select, order_by, group_by = self.pre_sql_setup()
File "~/.virtualenvs/dj18_sr/lib/python3.4/site-
packages/django/db/models/sql/compiler.py", line 48, in pre_sql_setup
self.setup_query()
File "~/.virtualenvs/dj18_sr/lib/python3.4/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 "~/.virtualenvs/dj18_sr/lib/python3.4/site-
packages/django/db/models/sql/compiler.py", line 184, in get_select
for c in self.get_default_columns():
File "~/.virtualenvs/dj18_sr/lib/python3.4/site-
packages/django/db/models/sql/compiler.py", line 487, in
get_default_columns
only_load = self.deferred_to_columns()
File "~/.virtualenvs/dj18_sr/lib/python3.4/site-
packages/django/db/models/sql/compiler.py", line 755, in
deferred_to_columns
self.query.deferred_to_data(columns,
self.query.get_loaded_field_names_cb)
File "~/.virtualenvs/dj18_sr/lib/python3.4/site-
packages/django/db/models/sql/query.py", line 622, in deferred_to_data
cur_model = source.rel.to
AttributeError: 'ManyToOneRel' object has no attribute 'rel'
}}}
Even though the error makes sense once you know why it's happening, I
think it would be more helpful to the programmer if the message stated
that one cannot use `only`/`defer` on a table/field that has had the
reverse relationship removed via `related_name="+"`
--
Ticket URL: <https://code.djangoproject.com/ticket/24612>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/051.2b1c7e0ca2e5631b751a22d1e303fd01%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.