Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#33739 closed Bug (invalid)

Migration on the test database doesn't work

Reported by: ThalusA Owned by: nobody
Component: Testing framework Version: 4.0
Severity: Normal Keywords: postgres unittest
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

"Any migrations will also be applied in order to keep it up to date." https://docs.djangoproject.com/en/4.0/topics/testing/overview/#the-test-database-1

The statement above is false, I will explain why.
First of all, here are all my versions:

  • PostgreSQL 14.2 (with PostGIS 3.2)
  • Django 4.0.4

I'm using unitary tests for my app using Django's TestCase. It is detected nicely, everything works according to plan except for one thing.

When I run ./manage.py test, Django creates another database called test_administrator because my database name is administrator but in this database, there is only the table for PostGIS data, and not all the models from administrator which are created using ./manage.py migrate.

My temporary fix is to run ./manage.py migrate on the database test_administrator and run the command ./manage.py test with the flag to keep the database because it deletes it each time I run it, elsewhere.

It would be lovely to have a fix on this issue.

Change History (6)

comment:1 by Mariusz Felisiak, 2 years ago

Severity: Release blockerNormal

Can you provide a sample project with your database configuration?

comment:2 by Mariusz Felisiak, 2 years ago

Resolution: needsinfo
Status: newclosed

in reply to:  1 comment:3 by ThalusA, 2 years ago

Replying to Mariusz Felisiak:

Can you provide a sample project with your database configuration?

My project is quite complex so I can give you its structure and database configuration

.
├── aact
│   ├── apps.py
│   ├── __init__.py
│   ├── management
│   │   ├── commands
│   │   │   ├── extract_transform_load.py
│   │   │   └── __init__.py
│   │   └── __init__.py
│   ├── models
│   │   ├── brief_summary.py
│   │   ├── calculated_values.py
│   │   ├── central_contact.py
│   │   ├── condition.py
│   │   ├── contact.py
│   │   ├── detailed_description.py
│   │   ├── eligibility.py
│   │   ├── facility.py
│   │   ├── __init__.py
│   │   ├── intervention.py
│   │   ├── investigator.py
│   │   ├── sponsor.py
│   │   └── trial.py
│   └── transformer
│       ├── age.py
│       ├── calculated_properties.py
│       ├── fields.py
│       ├── gender.py
│       ├── __init__.py
│       ├── links.py
│       ├── phase.py
│       ├── recruiting_status.py
│       └── trial.py
├── Aptfile
├── back
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── docker-compose.yml
├── launch.sh
├── manage.py
├── medical
│   ├── admin.py
│   ├── apps.py
│   ├── fixtures
│   │   ├── alterations.json
│   │   ├── biomarkers.json
│   │   ├── histologies.json
│   │   ├── __init__.py
│   │   ├── mechanisms_of_action.json
│   │   ├── molecule_types.json
│   │   ├── organs.json
│   │   ├── routes_of_administrationorgans.json
│   │   ├── situations.json
│   │   └── specialities.json
│   ├── __init__.py
│   ├── managers
│   │   ├── __init__.py
│   │   └── medical_alias_manager.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   └── __init__.py
│   ├── models
│   │   ├── alteration.py
│   │   ├── biomarker.py
│   │   ├── histology.py
│   │   ├── __init__.py
│   │   ├── mechanism_of_action.py
│   │   ├── molecule_type.py
│   │   ├── organ.py
│   │   ├── route_of_administration.py
│   │   ├── situation.py
│   │   └── speciality.py
│   ├── serializers
│   │   ├── alteration.py
│   │   ├── biomarker.py
│   │   ├── histology.py
│   │   ├── __init__.py
│   │   ├── mechanism_of_action.py
│   │   ├── molecule_type.py
│   │   ├── organ.py
│   │   ├── route_of_administration.py
│   │   ├── situation.py
│   │   └── speciality.py
│   ├── tests.py
│   ├── translation.py
│   ├── urls.py
│   └── views
│       ├── alteration.py
│       ├── biomarker.py
│       ├── histology.py
│       ├── __init__.py
│       ├── mechanism_of_action.py
│       ├── molecule_type.py
│       ├── organ.py
│       ├── route_of_administration.py
│       ├── situation.py
│       └── speciality.py
├── organization
│   ├── admin.py
│   ├── apps.py
│   ├── enums
│   │   ├── __init__.py
│   │   ├── recruiting_status.py
│   │   └── slots.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   └── __init__.py
│   ├── models
│   │   ├── facility.py
│   │   ├── __init__.py
│   │   └── sponsor.py
│   ├── serializers
│   │   ├── facility.py
│   │   ├── __init__.py
│   │   └── sponsor.py
│   ├── tests.py
│   ├── urls.py
│   └── views
│       ├── facility.py
│       ├── __init__.py
│       └── sponsor.py
├── patient
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0002_initial.py
│   │   └── __init__.py
│   ├── models
│   │   ├── __init__.py
│   │   ├── patient_form.py
│   │   └── pre_screening_decision.py
│   ├── serializers
│   │   ├── __init__.py
│   │   ├── patient_form.py
│   │   └── pre_screening_decision.py
│   ├── tests.py
│   ├── urls.py
│   └── views
│       ├── __init__.py
│       └── patient_form.py
├── Procfile
├── README.md
├── requirements.txt
├── resources
│   ├── apps.py
│   ├── fixtures
│   │   ├── cancer_synonyms.json
│   │   ├── cities.json
│   │   ├── facilities_aliases.json
│   │   ├── line_synonyms.json
│   │   ├── organs_specialities.json
│   │   ├── prioritized_terms.json
│   │   └── standard_facilities.json
│   ├── __init__.py
│   ├── managers
│   │   ├── alias_manager.py
│   │   ├── __init__.py
│   │   └── priority_manager.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   └── __init__.py
│   ├── models
│   │   ├── cancer_synonym.py
│   │   ├── city.py
│   │   ├── facility_alias.py
│   │   ├── __init__.py
│   │   ├── line_synonym.py
│   │   ├── organ_speciality.py
│   │   ├── prioritized_terms.py
│   │   └── standard_facility.py
│   ├── serializers
│   │   ├── __init__.py
│   │   └── standard_facility.py
│   ├── solvers
│   │   ├── __init__.py
│   │   ├── priority_resolver.py
│   │   └── regex_resolver.py
│   ├── urls.py
│   └── views
│       ├── __init__.py
│       └── standard_facility.py
├── resources.sh
├── runtime.txt
├── scheduler.py
├── secret_key.py
├── trial
│   ├── admin.py
│   ├── apps.py
│   ├── enums
│   │   ├── adjuvant_or_neo_adjuvant.py
│   │   ├── brain_metastasis.py
│   │   ├── field.py
│   │   ├── gender.py
│   │   ├── __init__.py
│   │   ├── partnership.py
│   │   ├── phase.py
│   │   ├── poc.py
│   │   ├── recist_measurable.py
│   │   └── review_status.py
│   ├── fixtures
│   │   └── specific_criterion.json
│   ├── __init__.py
│   ├── management
│   │   ├── commands
│   │   │   ├── __init__.py
│   │   │   └── loadditep.py
│   │   └── __init__.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0002_initial.py
│   │   ├── 0003_external.py
│   │   ├── 0004_cohort_organs.py
│   │   ├── 0004_specificcriterion_specificcriterionvalue.py
│   │   ├── 0004_trial_comments.py
│   │   ├── 0005_alter_specificcriterion_question_hint.py
│   │   ├── 0005_merge_0004_cohort_organs_0004_trial_comments.py
│   │   ├── 0006_merge_20220518_1417.py
│   │   ├── 0006_trial_brief_summary_trial_brief_summary_en_and_more.py
│   │   ├── 0007_alter_cohort_organs.py
│   │   ├── 0008_alter_cohort_organs.py
│   │   ├── 0009_merge_20220519_1116.py
│   │   ├── 0010_alter_trialbiomarker_unique_together.py
│   │   └── __init__.py
│   ├── models
│   │   ├── cohort.py
│   │   ├── condition.py
│   │   ├── contact.py
│   │   ├── external.py
│   │   ├── __init__.py
│   │   ├── intervention.py
│   │   ├── other_exclusion_criterion.py
│   │   ├── other_inclusion_criterion.py
│   │   ├── specific_criterion.py
│   │   ├── trial_biomarker.py
│   │   ├── trial_facility.py
│   │   └── trial.py
│   ├── serializers
│   │   ├── cohort.py
│   │   ├── condition.py
│   │   ├── contact.py
│   │   ├── external
│   │   │   ├── gustave_roussy.py
│   │   │   └── __init__.py
│   │   ├── __init__.py
│   │   ├── intervention.py
│   │   ├── other_exclusion_criterion.py
│   │   ├── other_inclusion_criterion.py
│   │   ├── specific_criterion.py
│   │   ├── trial_biomarker.py
│   │   ├── trial_facility.py
│   │   ├── trial.py
│   │   └── trial_search.py
│   ├── tests
│   │   ├── __init__.py
│   │   └── test_trial.py
│   ├── translation.py
│   ├── urls.py
│   └── views
│       ├── external
│       │   ├── gustave_roussy.py
│       │   └── __init__.py
│       ├── __init__.py
│       ├── specific_criterion.py
│       ├── trial.py
│       └── trial_search.py
└── user
    ├── admin.py
    ├── apps.py
    ├── enums
    │   ├── __init__.py
    │   └── user_type.py
    ├── __init__.py
    ├── management
    │   ├── commands
    │   │   ├── create_fake_users.py
    │   │   ├── create_superuser_with_password.py
    │   │   ├── __init__.py
    │   │   └── token.py
    │   └── __init__.py
    ├── migrations
    │   ├── 0001_initial.py
    │   ├── 0002_alter_user_options.py
    │   └── __init__.py
    ├── models
    │   ├── anonymous.py
    │   ├── base_user.py
    │   ├── doctor.py
    │   ├── __init__.py
    │   ├── investigator.py
    │   ├── patient.py
    │   ├── referrent.py
    │   └── sponsor.py
    ├── permissions
    │   ├── anonymous_permissions.py
    │   ├── custom_permissions.py
    │   ├── __init__.py
    │   ├── investigator_permissions.py
    │   ├── patient_permissions.py
    │   ├── referent_permissions.py
    │   └── sponsor_permissions.py
    ├── serializers
    │   ├── base_user.py
    │   ├── __init__.py
    │   ├── investigator.py
    │   ├── patient.py
    │   ├── referent.py
    │   └── sponsor.py
    ├── tests.py
    ├── urls.py
    └── views
        ├── base_user.py
        ├── __init__.py
        ├── investigator.py
        ├── patient.py
        ├── referent.py
        └── sponsor.py

53 directories, 267 files    
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

if 'DATABASE_URL' in os.environ:
    DATABASES = {
        'default': dj_database_url.parse(os.environ.get('DATABASE_URL'))
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': env('A_DATABASE_NAME'),
            'USER': env('A_DATABASE_USER'),
            'PASSWORD': env('A_DATABASE_PASS'),
            'HOST': env('A_DATABASE_HOST'),
            'PORT': env('A_DATABASE_PORT'),
        },
    }
DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'

DATABASES['b'] = {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'OPTIONS': {
        'options': '-c search_path=ctgov'
    },
    'NAME': env('B_DATABASE_NAME'),
    'USER': env('B_DATABASE_USER'),
    'PASSWORD': env('B_DATABASE_PASS'),
    'HOST': env('B_DATABASE_HOST'),
    'PORT': env('B_DATABASE_PORT'),
}

comment:4 by Mariusz Felisiak, 2 years ago

My project is quite complex so I can give you its structure and database configuration

Thanks, unfortunately it's not enough to confirm a bug in Django. Your issue may be related with lack of support for database schemas, see #6148, but it's hard to say without a reproducible scenario. Please reopen the ticket if you can debug your issue and provide a minimal project that reproduces the issue.

comment:5 by ThalusA, 2 years ago

Resolution: needsinfofixed

My problem was a misconfiguration of my settings. There was a conflict between my custom user model and rest_framework's authtoken. So I only had to touch the INSTALLED_APPS settings

Last edited 2 years ago by ThalusA (previous) (diff)

comment:6 by Mariusz Felisiak, 2 years ago

Resolution: fixedinvalid

Thanks for the follow-up.

Note: See TracTickets for help on using tickets.
Back to Top