wemake-services / django-test-migrations

Test django schema and data migrations, including migrations' order and best practices.

Home Page:https://pypi.org/project/django-test-migrations/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"Relation {table} does not exist" when creating using prototype model

benwad opened this issue · comments

Hi, I couldn't find anything about this issue so I thought I'd post it here. I have a test like this:

@pytest.mark.django_db
def test_0104_migration(migrator_factory):
    migrator = migrator_factory("default")
    old_state = migrator.apply_initial_migration(("my_app", "0103_previous_migration"))
    Overlay = old_state.apps.get_model("my_app", "Overlay")
    OverlayVersion = old_state.apps.get_model("my_app", "OverlayVersion")
    Video = old_state.apps.get_model("my_other_app", "Video")

    video_1 = Video.objects.create() # This is where the error happens
    overlay_no_current_version = Overlay.objects.create(video=video_1,)

This gives me the error: django.db.utils.ProgrammingError: relation "my_app_video" does not exist.

I also get the following error during teardown: psycopg2.errors.UndefinedTable: relation "django_content_type" does not exist

The test is running inside docker, with Postgres as the DB backend. Is there something I'm missing with apply_initial_migration which might not be creating these tables? I also tried Overlay.objects.create and OverlayVersion.objects.create and I get the same error. How could I go about debugging this? The migration itself works, and I've rebuilt the whole container and run the whole migration graph a number of times.

Hi @benwad,

This kind of error usually happens when the migrations have invalid dependencies defined. Please double-check that the migration which populates your Video model is executed.

It will be really helpful if you can prepare the most straightforward code (models, tests, migrations, etc) that is still causing this issue, so we can debug it, and paste a full traceback, so we can have more context.

Thanks for the reply! I'll take a look at the migrations graph. manage.py migrate works fine, but we do have quite a big dependency graph and have done some squashes etc so there could be something strange in there.