Search code examples
python-3.xdjangographqldjango-migrationsgraphene-django

graphene code run before django data migrations


I wrote a piece of code that generates graphene input object type dynamically from the database. when I try to run

./manage.py migrate

that code runs before the migration and caused

django.db.utils.ProgrammingError

I have the same issue in run the Pytest too. how can I prevent this code from running before the data migrations


Solution

  • i found this code on the pytest django GitHub issue to unblock database restriction in py test hook.

    import pytest
    from pytest_django.plugin import _blocking_manager
    from django.db.backends.base.base import BaseDatabaseWrapper
    
    
    @pytest.hookimpl(tryfirst=True)
    def pytest_load_initial_conftests(early_config, parser, args):
        _blocking_manager.unblock()
        _blocking_manager._blocking_wrapper = BaseDatabaseWrapper.ensure_connection
    

    and call this hook in pytest.ini with -p argument