[Python] Can't teardown django test database if async queries are used

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    I have a django project with postgres database configured. Here is the relevant part of requirements.txt:

    Django==5.0.1
    psycopg2-binary==2.9.9


    Here I have one model defined:

    class Item(models.Model):
    name = models.CharField(max_length=100)


    And a test case:

    class ItemTestCase(TestCase):
    def test_create(self):
    asyncio.run(Item.objects.acreate(name="Test"))
    self.assertEqual(Item.objects.count(), 1)


    Issue:

    When I'm running the tests using python manage.py test - tests are passed but at the end i see an exception that test runner can't destroy test database.

    2024-01-18 19:05:03 django.db.utils.OperationalError: database "test_axpress" is being accessed by other users
    2024-01-18 19:05:03 DETAIL: There is 1 other session using the database.


    Is there a way to ensure that all connections are closed before tearing down the database?

    Continue reading...

Compartilhe esta Página