#32197: Misleading error when loading a database backend fails with ImportError
-------------------------------------+-------------------------------------
               Reporter:  Tim        |          Owner:  nobody
  Graham                             |
                   Type:             |         Status:  new
  Cleanup/optimization               |
              Component:  Database   |        Version:  master
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  1
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 For example, in the following case, `django_cockroachdb` is installed but
 has an import error itself.
 {{{
 Traceback (most recent call last):
   File "/home/tim/code/django/django/db/utils.py", line 110, in
 load_backend
     return import_module('%s.base' % backend_name)
   File
 "/home/tim/.virtualenvs/django39/lib/python3.9/importlib/__init__.py",
 line 127, in import_module
     return _bootstrap._gcd_import(name[level:], package, level)
   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
   File "<frozen importlib._bootstrap>", line 972, in
 _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 228, in
 _call_with_frames_removed
   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
   File "<frozen importlib._bootstrap>", line 986, in
 _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
   File "<frozen importlib._bootstrap_external>", line 790, in exec_module
   File "<frozen importlib._bootstrap>", line 228, in
 _call_with_frames_removed
   File "/home/tim/code/cockroach-django/django_cockroachdb/__init__.py",
 line 2, in <module>
     from .lookups import patch_lookups
   File "/home/tim/code/cockroach-django/django_cockroachdb/lookups.py",
 line 1, in <module>
     from django.db.models.fields.json import HasKeyLookup, KeyTransform
 ModuleNotFoundError: No module named 'django.db.models.fields.json'

 The above exception was the direct cause of the following exception:

 Traceback (most recent call last):
   File "/home/tim/code/mysite/manage.py", line 10, in <module>
     execute_from_command_line(sys.argv)
   File "/home/tim/code/django/django/core/management/__init__.py", line
 381, in execute_from_command_line
     utility.execute()
   File "/home/tim/code/django/django/core/management/__init__.py", line
 357, in execute
     django.setup()
   File "/home/tim/code/django/django/__init__.py", line 24, in setup
     apps.populate(settings.INSTALLED_APPS)
   File "/home/tim/code/django/django/apps/registry.py", line 114, in
 populate
     app_config.import_models()
   File "/home/tim/code/django/django/apps/config.py", line 211, in
 import_models
     self.models_module = import_module(models_module_name)
   File
 "/home/tim/.virtualenvs/django39/lib/python3.9/importlib/__init__.py",
 line 127, in import_module
     return _bootstrap._gcd_import(name[level:], package, level)
   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
   File "<frozen importlib._bootstrap>", line 986, in
 _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
   File "<frozen importlib._bootstrap_external>", line 790, in exec_module
   File "<frozen importlib._bootstrap>", line 228, in
 _call_with_frames_removed
   File "/home/tim/code/mysite/polls/models.py", line 12, in <module>
     class Question(models.Model):
   File "/home/tim/code/django/django/db/models/base.py", line 117, in
 __new__
     new_class.add_to_class('_meta', Options(meta, app_label))
   File "/home/tim/code/django/django/db/models/base.py", line 321, in
 add_to_class
     value.contribute_to_class(cls, name)
   File "/home/tim/code/django/django/db/models/options.py", line 204, in
 contribute_to_class
     self.db_table = truncate_name(self.db_table,
 connection.ops.max_name_length())
   File "/home/tim/code/django/django/db/__init__.py", line 28, in
 __getattr__
     return getattr(connections[DEFAULT_DB_ALIAS], item)
   File "/home/tim/code/django/django/db/utils.py", line 201, in
 __getitem__
     backend = load_backend(db['ENGINE'])
   File "/home/tim/code/django/django/db/utils.py", line 121, in
 load_backend
     raise ImproperlyConfigured(
 django.core.exceptions.ImproperlyConfigured: 'django_cockroachdb' isn't an
 available database backend.
 Try using 'django.db.backends.XXX', where XXX is one of:
     'mysql', 'oracle', 'postgresql', 'sqlite3'
 }}}
 I'm not sure there's anything that could be done besides rephrasing the
 error message to say something like this:

  'django_cockroachdb' isn't an available database backend or couldn't be
 imported. Check the above exception.

 In this case, "Try using..." isn't so helpful either. Perhaps we could
 only include that if `backend_name` begins with `django.`.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32197>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.ca03a89d6aab5d3c28dbc9446346298f%40djangoproject.com.

Reply via email to