pytest-django icon indicating copy to clipboard operation
pytest-django copied to clipboard

custom migration fail to migrate

Open amirreza8002 opened this issue 8 months ago • 5 comments
trafficstars

hi i wrote a custom migration file to add some data to a porject it looks like this:

from django.db import migrations


def make_teacher_group(apps, schema_editor):
    Group = apps.get_model("auth", "Group")
    Permission = apps.get_model("auth", "Permission")
    group = Group.objects.create(name="teachers")
    permission = Permission.objects.get(codename="teacher")
    group.permissions.add(permission)


def delete_teacher_group(apps, schema_editor):
    Group = apps.get_model("auth", "Group")
    group = Group.objects.get(name="teachers")
    group.permissions.clear()
    group.delete()


class Migration(migrations.Migration):

    dependencies = [
        ("accounts", "0003_user_purchase_number_teacherdata"),
    ]

    operations = [
        migrations.RunPython(make_teacher_group, delete_teacher_group),
    ]

when running pytest, it fails to migrate this with the following error:

============================================================================================================ test session starts =============================================================================================================
platform linux -- Python 3.12.8, pytest-8.3.5, pluggy-1.5.0
django: version: 5.1.7, settings: jozvenama_project.settings (from ini)
rootdir: /home/amirreza/projects/jozvenama/backend
configfile: pyproject.toml
plugins: django-4.10.0, env-1.1.5
collected 14 items                                                                                                                                                                                                                           

tests/test_accounts/test_models.py E

=================================================================================================================== ERRORS ===================================================================================================================
________________________________________________________________________________________ ERROR at setup of TestUserModel.test_valid_data_creates_user ________________________________________________________________________________________

request = <SubRequest '_django_db_marker' for <Function test_valid_data_creates_user>>

    @pytest.fixture(autouse=True)
    def _django_db_marker(request: pytest.FixtureRequest) -> None:
        """Implement the django_db marker, internal to pytest-django."""
        marker = request.node.get_closest_marker("django_db")
        if marker:
>           request.getfixturevalue("_django_db_helper")

.venv/lib/python3.12/site-packages/pytest_django/plugin.py:552: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.venv/lib/python3.12/site-packages/pytest_django/fixtures.py:144: in django_db_setup
    db_cfg = setup_databases(
.venv/lib/python3.12/site-packages/django/test/utils.py:206: in setup_databases
    connection.creation.create_test_db(
.venv/lib/python3.12/site-packages/django/db/backends/base/creation.py:78: in create_test_db
    call_command(
.venv/lib/python3.12/site-packages/django/core/management/__init__.py:194: in call_command
    return command.execute(*args, **defaults)
.venv/lib/python3.12/site-packages/django/core/management/base.py:459: in execute
    output = self.handle(*args, **options)
.venv/lib/python3.12/site-packages/django/core/management/base.py:107: in wrapper
    res = handle_func(*args, **kwargs)
.venv/lib/python3.12/site-packages/django/core/management/commands/migrate.py:357: in handle
    post_migrate_state = executor.migrate(
.venv/lib/python3.12/site-packages/django/db/migrations/executor.py:135: in migrate
    state = self._migrate_all_forwards(
.venv/lib/python3.12/site-packages/django/db/migrations/executor.py:167: in _migrate_all_forwards
    state = self.apply_migration(
.venv/lib/python3.12/site-packages/django/db/migrations/executor.py:255: in apply_migration
    state = migration.apply(state, schema_editor)
.venv/lib/python3.12/site-packages/django/db/migrations/migration.py:132: in apply
    operation.database_forwards(
.venv/lib/python3.12/site-packages/django/db/migrations/operations/special.py:196: in database_forwards
    self.code(from_state.apps, schema_editor)
accounts/migrations/0004_teacher_group_logic.py:10: in make_teacher_group
    permission = Permission.objects.get(codename="teacher")
.venv/lib/python3.12/site-packages/django/db/models/manager.py:87: in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <[RuntimeError('Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.') raised in repr()] QuerySet object at 0x7669610838f0>, args = (), kwargs = {'codename': 'teacher'}
clone = <QuerySet []>, limit = 21, num = 0

    def get(self, *args, **kwargs):
        """
        Perform the query and return a single object matching the given
        keyword arguments.
        """
        if self.query.combinator and (args or kwargs):
            raise NotSupportedError(
                "Calling QuerySet.get(...) with filters after %s() is not "
                "supported." % self.query.combinator
            )
        clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
        if self.query.can_filter() and not self.query.distinct_fields:
            clone = clone.order_by()
        limit = None
        if (
            not clone.query.select_for_update
            or connections[clone.db].features.supports_select_for_update_with_limit
        ):
            limit = MAX_GET_RESULTS
            clone.query.set_limits(high=limit)
        num = len(clone)
        if num == 1:
            return clone._result_cache[0]
        if not num:
>           raise self.model.DoesNotExist(
                "%s matching query does not exist." % self.model._meta.object_name
            )
E           __fake__.Permission.DoesNotExist: Permission matching query does not exist.

.venv/lib/python3.12/site-packages/django/db/models/query.py:649: DoesNotExist

========================================================================================================== short test summary info ===========================================================================================================
ERROR tests/test_accounts/test_models.py::TestUserModel::test_valid_data_creates_user - __fake__.Permission.DoesNotExist: Permission matching query does not exist.

as you can see, self = <[RuntimeError('Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.') raised in repr()] QuerySet object at 0x7669610838f0>, args = (), kwargs = {'codename': 'teacher'}

amirreza8002 avatar Mar 09 '25 01:03 amirreza8002