pytest-dev / pytest-dev/pytest-django

Tables in test db are not created for models with app_label

Open
#1,134 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

I use --no-migrations option to create a test database by inspecting all models. This usually works well, but I got a problem with django models that have an app_label meta attr. Such models are ignored and do not create automatically.

Model for example:

class SAPDepartment(models.Model):
    class Meta:
        app_label = 'structure'

    sap_name = models.CharField(max_length=150, null=False, blank=False)
    sap_id = models.IntegerField()
    up_dep = models.ForeignKey('self', related_name='low_dep', on_delete=models.SET_NULL, null=True)

Tables list if we do not use app_label (sapdepartment table exists):

test_procurement_db=# \dt
                         List of relations
 Schema |                 Name                  | Type  |  Owner
--------+---------------------------------------+-------+----------
...
 public | django_content_type                   | table | postgres
 public | django_session                        | table | postgres
 public | info_db_sapdepartment                 | table | postgres
 public | info_db_sapemployee                   | table | postgres
 public | material_analogue_employee            | table | postgres
...
(27 rows)

Tables list if we use app_label (there is not sapdepartment table):

test_procurement_db=# \dt
            List of relations
 Schema |                 Name                  | Type  |  Owner
--------+---------------------------------------+-------+----------
 public | auth_group                            | table | postgres
 public | auth_group_permissions                | table | postgres
 public | auth_permission                       | table | postgres
 public | auth_user                             | table | postgres
 public | auth_user_groups                      | table | postgres
 public | auth_user_user_permissions            | table | postgres
 public | django_admin_log                      | table | postgres
 public | django_celery_beat_clockedschedule    | table | postgres
 public | django_celery_beat_crontabschedule    | table | postgres
 public | django_celery_beat_intervalschedule   | table | postgres
 public | django_celery_beat_periodictask       | table | postgres
 public | django_celery_beat_periodictasks      | table | postgres
 public | django_celery_beat_solarschedule      | table | postgres
 public | django_content_type                   | table | postgres
 public | django_session                        | table | postgres
 public | material_analogue_employee            | table | postgres
 public | material_analogue_group               | table | postgres
 public | material_analogue_group_members       | table | postgres
 public | material_analogue_lot                 | table | postgres
 public | material_analogue_materialrequest     | table | postgres
 public | material_analogue_materialrequestfile | table | postgres
 public | material_analogue_procurer            | table | postgres
 public | material_analogue_requeststatus       | table | postgres
 public | material_analogue_subsection          | table | postgres
 public | material_analogue_supplier            | table | postgres
(25 rows)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with pytest-django's --no-migrations option and the SAPDepartment model using Meta.app_label. Trace the test-database setup path that inspects models, then verify that the model's table is created while preserving the existing behavior for models without app_label.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
databases, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.