Opened 5 months ago

Closed 5 months ago

Last modified 5 months ago

#35545 closed Bug (fixed)

Fix create_permissions LookupErrors

Reported by: Csirmaz Bendegúz Owned by: Sarah Boyce
Component: contrib.auth Version: 5.1
Severity: Release blocker Keywords:
Cc: Adam Johnson, Mariusz Felisiak, Simon Charette Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Csirmaz Bendegúz)

#35408 introduced a bug in contrib.auth's create_permissions function (PR, commit d2c5a30e5a8595d06c4a70ff0f66032fee0d3c8e).
Since this function is called by the post_migrate signal, it affects the migrate command.
The function raises unexpected LookupError s.

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, dev, sessions
Running migrations:
  Applying dev.0001_initial... OK
Traceback (most recent call last):
  File "C:\Users\csirm\PycharmProjects\django-dev\venv\Lib\site-packages\django\apps\registry.py", line 158, in get_app_config
    return self.app_configs[app_label]
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^
KeyError: 'dev'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\csirm\PycharmProjects\django-dev\django_dev\manage.py", line 22, in <module>
    main()
  File "C:\Users\csirm\PycharmProjects\django-dev\django_dev\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\csirm\PycharmProjects\django-dev\venv\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "C:\Users\csirm\PycharmProjects\django-dev\venv\Lib\site-packages\django\core\management\__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\csirm\PycharmProjects\django-dev\venv\Lib\site-packages\django\core\management\base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\csirm\PycharmProjects\django-dev\venv\Lib\site-packages\django\core\management\base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\csirm\PycharmProjects\django-dev\venv\Lib\site-packages\django\core\management\base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\csirm\PycharmProjects\django-dev\venv\Lib\site-packages\django\core\management\commands\migrate.py", line 384, in handle
    emit_post_migrate_signal(
  File "C:\Users\csirm\PycharmProjects\django-dev\venv\Lib\site-packages\django\core\management\sql.py", line 52, in emit_post_migrate_signal
    models.signals.post_migrate.send(
  File "C:\Users\csirm\PycharmProjects\django-dev\venv\Lib\site-packages\django\dispatch\dispatcher.py", line 189, in send
    response = receiver(signal=self, sender=sender, **named)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\csirm\PycharmProjects\django-dev\venv\Lib\site-packages\django\contrib\auth\management\__init__.py", line 78, in create_permissions
    ctypes = ContentType.objects.db_manager(using).get_for_models(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\csirm\PycharmProjects\django-dev\venv\Lib\site-packages\django\contrib\contenttypes\models.py", line 93, in get_for_models
    ct._meta.apps.get_model(ct.app_label, ct.model)._meta, []
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\csirm\PycharmProjects\django-dev\venv\Lib\site-packages\django\apps\registry.py", line 208, in get_model
    app_config = self.get_app_config(app_label)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\csirm\PycharmProjects\django-dev\venv\Lib\site-packages\django\apps\registry.py", line 165, in get_app_config
    raise LookupError(message)
LookupError: No installed app with label 'dev'.

Change History (9)

comment:1 by Csirmaz Bendegúz, 5 months ago

Description: modified (diff)

comment:2 by Natalia Bidart, 5 months ago

Cc: Adam Johnson added
Triage Stage: UnreviewedAccepted

Thank you Csirmaz for this report!

I can confirm I can reproduce in a simple Django project:

$  python -Wall manage.py migrate generator
Operations to perform:
  Apply all migrations: generator
Running migrations:
  Applying generator.0001_initial... OK
Traceback (most recent call last):
  File "/home/nessita/fellowship/django/django/apps/registry.py", line 158, in get_app_config
    return self.app_configs[app_label]
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^
KeyError: 'generator'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/nessita/fellowship/checklist-generator/manage.py", line 22, in <module>
    main()
  File "/home/nessita/fellowship/checklist-generator/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/nessita/fellowship/django/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/home/nessita/fellowship/django/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/nessita/fellowship/django/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/nessita/fellowship/django/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/nessita/fellowship/django/django/core/management/base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/nessita/fellowship/django/django/core/management/commands/migrate.py", line 384, in handle
    emit_post_migrate_signal(
  File "/home/nessita/fellowship/django/django/core/management/sql.py", line 52, in emit_post_migrate_signal
    models.signals.post_migrate.send(
  File "/home/nessita/fellowship/django/django/dispatch/dispatcher.py", line 189, in send
    response = receiver(signal=self, sender=sender, **named)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/nessita/fellowship/django/django/contrib/auth/management/__init__.py", line 78, in create_permissions
    ctypes = ContentType.objects.db_manager(using).get_for_models(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/nessita/fellowship/django/django/contrib/contenttypes/models.py", line 93, in get_for_models
    ct._meta.apps.get_model(ct.app_label, ct.model)._meta, []
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/nessita/fellowship/django/django/apps/registry.py", line 208, in get_model
    app_config = self.get_app_config(app_label)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/nessita/fellowship/django/django/apps/registry.py", line 165, in get_app_config
    raise LookupError(message)
LookupError: No installed app with label 'generator'.

comment:3 by Csirmaz Bendegúz, 5 months ago

Description: modified (diff)

comment:4 by Sarah Boyce, 5 months ago

Details for people investigating:

  • django.contrib.auth, django.contrib.contenttypes and a third app (app1) needs to be installed
  • migrate (important)
  • create a new model in app1
  • makemigrations and migrate

If you were to delete the SQLite db and apply the migrations, it would run fine which makes me think we have a caching issue in ContentTypeManager.get_for_models() (#31357, #32833)

comment:5 by Sarah Boyce, 5 months ago

Cc: Mariusz Felisiak Simon Charette added

comment:6 by Sarah Boyce, 5 months ago

Has patch: set
Owner: changed from nobody to Sarah Boyce
Status: newassigned

comment:7 by Sarah Boyce, 5 months ago

Triage Stage: AcceptedReady for checkin

comment:8 by Sarah Boyce <42296566+sarahboyce@…>, 5 months ago

Resolution: fixed
Status: assignedclosed

In f1705c8:

Fixed #35545, Refs #32833 -- Fixed ContentTypeManager.get_for_models() crash in CreateModel migrations.

Thank you to Csirmaz Bendegúz for the report and Simon Charettes for the review.

comment:9 by Sarah Boyce <42296566+sarahboyce@…>, 5 months ago

In 6317803:

[5.1.x] Fixed #35545, Refs #32833 -- Fixed ContentTypeManager.get_for_models() crash in CreateModel migrations.

Thank you to Csirmaz Bendegúz for the report and Simon Charettes for the review.

Backport of f1705c8780c0a7587654fc736542d55fe4a7f29b from main.

Note: See TracTickets for help on using tickets.
Back to Top