Skip to content

Need to clear caches in django.db.connections during django_db_modify_db_settings? #587

Open
@Code0x58

Description

@Code0x58

ConnectionHandler (django.db.connections is an instance of this) caches, so I ended up with with problems due to using connection from the old config† after changing settings from a django_db_modify_db_settings fixture. I fixed my issue by calling the following code before+after the fixture:

def clear_caches():
    """Invalidate the connection caches."""
    connections._connections = threading.local()
    # this will clear the cached property
    del connections.databases

If there isn't any disagreement I will submit a PR for this to change the default fixture and also update the documentation.

This looks like it should work back to at least 1.7.x, I haven't checked back further. Is there an officially supported range of Django versions?

† in my case I was only getting the database which was configured in DJANGO_SETTINGS_MODULE, I am not sure if that was because of app code causing a query too soon, or if it is expected every time in Django (in 1.11.11)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions