4

After deleting an old Model in my Django micro-service, pytest fails to setup the test DB. The migrations fail to execute correctly. However, the migration generated after the model deletion works well on my staging DB. The problem is only impacting new DBs and in this case, the one created by pytest.

What I did to delete my model:

  1. Remove all use cases of my model
  2. Delete my model
  3. Makemigrations
  4. Migrate

The pytest DB-creation step is replaying the migrations history. It should:

  1. Create my model
  2. Define constraints
  3. Delete my model as I want to delete it

The error makes me think the creation of the model is deferred as Django see that it is deleted later, and so the model constraints seem to be executed before the creation of the model.

self = <django.db.backends.utils.CursorWrapper object at 0x114590198>
sql = 'ALTER TABLE "catalogue_productoffer_competitors" ADD CONSTRAINT "catalogue_productoff_competitor_id_99767623_fk_categorie" FOREIGN KEY ("competitor_id") REFERENCES "categories_competitor" ("id") DEFERRABLE INITIALLY DEFERRED'
django.db.utils.ProgrammingError: relation "categories_competitor" does not exist

Thanks for your help !

  • This might be an issue with your migration file not properly reflecting the right order of operations. Do you get the same error when starting a development setup from scratch (outside of any tests) ? – Samuel Dion-Girardeau Feb 10 '19 at 16:24
  • I've just run into something similar, myself. I'm curious if you remember how you resolved this. – 780Farva Jun 27 '21 at 20:22

0 Answers0