Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my Django migrations loading my urls.py?

I'm trying to run django migrations on a freshly installed Heroku instance but was getting a ProgrammingError. The error was due to some module-level queries that were being performed in a totally separate module and shouldn't be called at all during the migration.

It turns out that the reason they files were being called is because they were in a file that was imported into my urls.py and, for some reason, Django was loading those urls.

Is there some reason Django must load the urls even though migrations don't depend on them, and is there any way to prevent them from being loaded?

like image 765
Soviut Avatar asked Jul 14 '16 20:07

Soviut


People also ask

What is the difference between Makemigrations and migrate in Django?

makemigrations is responsible for packaging up your model changes into individual migration files - analogous to commits - and migrate is responsible for applying those to your database.

What does fake migration do in Django?

--fake-initialAllows Django to skip an app's initial migration if all database tables with the names of all models created by all CreateModel operations in that migration already exist. This option is intended for use when first running migrations against a database that preexisted the use of migrations.

Should I delete migrations Django?

If you've not created some custom migrations for like loading data, then yes. It should be safe to remove all of the migrations in the migrations folder and run makemigratons command.


1 Answers

There is a boolean class attribute in BaseCommand class called requires_system_checks, which is True by default. It will check all potential problems prior to executing the command. In the 3.0 version, there is a flag called --skip-checks which skips running system checks prior to running the command. I checked on a brand new generated Django project and it worked without raising the expected exception that I wrote intentionally in the urls.py module.

like image 51
Davit Tovmasyan Avatar answered Sep 18 '22 15:09

Davit Tovmasyan