Skip to content

No way to specify the database for fixtures #1192

@xi

Description

@xi

The only way to declare that a fixture requires a database is the db fixture. However, since #1186 this will only pull in the default database. As far as I can see, there is currently no way to specify a different database.

Prior to #1186, the db fixture pulled in all databases.

Activity

added a commit that references this issue on Apr 14, 2025
98e1628
added a commit that references this issue on Apr 14, 2025
dc8aa0d
xi

xi commented on Apr 15, 2025

@xi
Author

@pytest.mark.django_db cannot be used for fixtures, so it does not apply here. The only way to declare that a fixture requires a database is the db fixture, and that does not take any parameters.

terencehonles

terencehonles commented on May 21, 2025

@terencehonles

I'm also running into this issue.

I was hoping not to auto mark all tests as using a DB and instead let the fixtures pull in a database as needed. This is not something that can be easily configured per fixture, but maybe returning the default set of DBs to pull in via a fixture would allow some customization.

sshishov

sshishov commented on Aug 13, 2025

@sshishov

We have another issues. Before our tests were running using test_<db> but now our main database is cleaned/overwritten as we are calling flush and loaddata to pre-populate some static data:

@pytest.fixture(scope='session')
def django_db_setup(django_db_setup: None, django_db_blocker: pytest_django.DjangoDbBlocker, django_db_createdb: bool) -> None:
    del django_db_setup
    if django_db_createdb:
        with django_db_blocker.unblock():
            call_command('flush', '--noinput')
            call_command('loaddata', *pathlib.Path().glob('tests/db_fixtures/**/*.yaml'))

And these commands are executed on <db> database.

Reverting to 4.10.0 fixes the issue.

Please note, that test_<db> is not even getting created...

kingbuzzman

kingbuzzman commented on Aug 13, 2025

@kingbuzzman
Member

Reverting to 4.10.0 fixes the issue.

Did you downgrade from 4.11.1?

What were you doing prior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @xi@kingbuzzman@terencehonles@bluetech@sshishov

      Issue actions

        No way to specify the database for fixtures · Issue #1192 · pytest-dev/pytest-django