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

`Database access not allowed` when passing function to default foreign key

Open buddylindsey opened this issue 4 years ago • 2 comments
trafficstars

I am getting the following error when I am setting a function as a default value for a foreign key. I have the decorator on many tests, but it doesn't even finish loading the first test with the decorator before exploding.

Failed: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.

Here is what I have:

class Score(models.Model):
    def default_value():
        return Sport.objects.get(game='football').id

    sport = models.ForeignKey(
        Sport,
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        default=default_value
    )
  1. This works with django since default is either looking for a value or a callable.
  2. It works in migrations since it is being called after all of the apps are initialized.
  3. It also just works in the normal course of using the project

I suspect this is just tripping up the order of something getting loaded.

buddylindsey avatar Apr 20 '21 22:04 buddylindsey

In general, I would strongly recommend against doing this (performing queries in default callback), if you can at all avoid it.

But in case you can't, can provide a stack trace of the failure? I would like to understand at which point the default is getting called.

bluetech avatar May 07 '21 10:05 bluetech

/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/models/base.py:533: in __init__
    val = field.get_default()
/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/models/fields/related.py:1111: in get_default
    field_default = super().get_default()
/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/models/fields/__init__.py:918: in get_default
    return self._get_default()
models/utils.py:208: in get_default_activity_type
    return cls.objects.get_or_create(default=True)[0]
/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/models/manager.py:85: in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/models/query.py:657: in get_or_create
    return self.get(**kwargs), False
/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/models/query.py:492: in get
    num = len(clone)
/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/models/query.py:302: in __len__
    self._fetch_all()
/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/models/query.py:1507: in _fetch_all
    self._result_cache = list(self._iterable_class(self))
/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/models/query.py:57: in __iter__
    results = compiler.execute_sql(
/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/models/sql/compiler.py:1361: in execute_sql
    cursor.execute(sql, params)
/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/backends/utils.py:103: in execute
    return super().execute(sql, params)
/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/backends/utils.py:67: in execute
    return self._execute_with_wrappers(
/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/backends/utils.py:80: in _execute_with_wrappers
    return executor(sql, params, many, context)
/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/backends/utils.py:84: in _execute
    with self.db.wrap_database_errors:
/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/utils.py:91: in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
/home/atonra/.cache/pypoetry/virtualenvs/wbcrm-gKdFzsyu-py3.10/lib/python3.10/site-packages/django/db/backends/utils.py:89: in _execute
    return self.cursor.execute(sql, params)
E   django.db.utils.ProgrammingError: relation "wbcrm_activitytype" does not exist
E   LINE 1: ...tytype"."score", "wbcrm_activitytype"."icon" FROM "wbcrm_act...

loickouao avatar Aug 04 '22 08:08 loickouao