﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34849	Appending django.contrib.postgres to the INSTALLED_APPS via @modify_settings crashes.	Mariusz Felisiak	David Sanders	"Appending `django.contrib.postgres` to the `INSTALLED_APPS` via `@modify_settings` crashes with:
{{{
RuntimeWarning: Accessing the database during app initialization is discouraged. To fix this warning, avoid executing queries in AppConfig.ready() or when your app modules are imported.
}}}
Check out:
{{{
./runtests.py postgres_tests
...
======================================================================
ERROR: test_register_serializer_for_migrations (postgres_tests.test_apps.PostgresConfigTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/usr/local/lib/python3.10/unittest/case.py"", line 59, in testPartExecutor
    yield
  File ""/usr/local/lib/python3.10/unittest/case.py"", line 591, in run
    self._callTestMethod(testMethod)
  File ""/usr/local/lib/python3.10/unittest/case.py"", line 549, in _callTestMethod
    method()
  File ""/django/tests/postgres_tests/test_apps.py"", line 64, in test_register_serializer_for_migrations
    with self.modify_settings(INSTALLED_APPS={""append"": ""django.contrib.postgres""}):
  File ""/django/django/test/utils.py"", line 405, in __enter__
    return self.enable()
  File ""/django/django/test/utils.py"", line 587, in enable
    super().enable()
  File ""/django/django/test/utils.py"", line 474, in enable
    apps.set_installed_apps(self.options[""INSTALLED_APPS""])
  File ""/django/django/apps/registry.py"", line 362, in set_installed_apps
    self.populate(installed)
  File ""/django/django/apps/registry.py"", line 124, in populate 
    app_config.ready()
  File ""/django/django/contrib/postgres/apps.py"", line 71, in ready
    register_type_handlers(conn)
  File ""/django/django/contrib/postgres/signals.py"", line 40, in register_type_handlers
    oids, array_oids = get_hstore_oids(connection.alias)
  File ""/django/django/contrib/postgres/signals.py"", line 24, in get_hstore_oids
    return get_type_oids(connection_alias, ""hstore"")
  File ""/django/django/contrib/postgres/signals.py"", line 10, in get_type_oids 
    cursor.execute(
  File ""/django/django/db/backends/utils.py"", line 77, in execute
    return self._execute_with_wrappers(
  File ""/django/django/db/backends/utils.py"", line 90, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ""/django/django/db/backends/utils.py"", line 94, in _execute
    warnings.warn(self.APPS_NOT_READY_WARNING_MSG, category=RuntimeWarning)
RuntimeWarning: Accessing the database during app initialization is discouraged. To fix this warning, avoid executing queries in AppConfig.ready() or when your app modules are imported.

======================================================================
ERROR: test_register_type_handlers_connection (postgres_tests.test_apps.PostgresConfigTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""/usr/local/lib/python3.10/unittest/case.py"", line 59, in testPartExecutor
    yield
  File ""/usr/local/lib/python3.10/unittest/case.py"", line 591, in run
    self._callTestMethod(testMethod)
  File ""/usr/local/lib/python3.10/unittest/case.py"", line 549, in _callTestMethod
    method()
  File ""/django/tests/postgres_tests/test_apps.py"", line 36, in test_register_type_handlers_connection
    with modify_settings(INSTALLED_APPS={""append"": ""django.contrib.postgres""}):
  File ""/django/django/test/utils.py"", line 405, in __enter__
    return self.enable()
  File ""/django/django/test/utils.py"", line 587, in enable
    super().enable()
  File ""/django/django/test/utils.py"", line 474, in enable
    apps.set_installed_apps(self.options[""INSTALLED_APPS""])
  File ""/django/django/apps/registry.py"", line 362, in set_installed_apps
    self.populate(installed)
  File ""/django/django/apps/registry.py"", line 124, in populate 
    app_config.ready()
  File ""/django/django/contrib/postgres/apps.py"", line 71, in ready
    register_type_handlers(conn)
  File ""/django/django/contrib/postgres/signals.py"", line 40, in register_type_handlers
    oids, array_oids = get_hstore_oids(connection.alias)
  File ""/django/django/contrib/postgres/signals.py"", line 24, in get_hstore_oids
    return get_type_oids(connection_alias, ""hstore"")
  File ""/django/django/contrib/postgres/signals.py"", line 10, in get_type_oids 
    cursor.execute(
  File ""/django/django/db/backends/utils.py"", line 77, in execute
    return self._execute_with_wrappers(
  File ""/django/django/db/backends/utils.py"", line 90, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File ""/django/django/db/backends/utils.py"", line 94, in _execute
    warnings.warn(self.APPS_NOT_READY_WARNING_MSG, category=RuntimeWarning)
RuntimeWarning: Accessing the database during app initialization is discouraged. To fix this warning, avoid executing queries in AppConfig.ready() or when your app modules are imported.

----------------------------------------------------------------------
Ran 590 tests in 1.840s

FAILED (errors=2, skipped=8, expected failures=3)
}}}

`@modify_settings` repopulates apps that register type handlers and hits the database to select OIDs (if not cached).

Regression in fbd16438f46bc2128926958ad24331da5d1b406f (#33143)."	Bug	closed	Database layer (models, ORM)	5.0	Release blocker	fixed		Adam Johnson Florian Zimmermann	Ready for checkin	1	0	0	0	0	0
