Changing a Django Model Breaks All Your Tests

I like to think that I learn from my mistakes. But there are a few things that creep up and bite me over and over. This is one of those errors. It gets me when I'm tired or distracted.

If you make a change to a Django model, and you do not create the migrations, when you run your tests all of your database tests will fail. If you are like me, you will experience a moment of deep despair as everything that used to work is now broken and the universe makes no sense. You'll think to yourself "What could I possibly have done to break everything?"

For me, running my tests with PyTest and working with PostgreSQL, after a long list of otuput I see an error like this one:

psycopg2.OperationalError: cursor "_django_curs_7196_10" does not exist

The cursor number is always different, but the cause is always the same. I made a change to some model and didn't make the migrations. It could be adding a new model, adding a field, or making some other model change. It will break all your database-backed tests even if they don't touch the changed model.

Luckily, the fix is easy. Just run makemigrations.

manage.py makemigrations

Rerun your tests, they pass, and suddenly the universe makes sense again.

Comments

Comments powered by Disqus