Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django makemigrations app order

I'm using Django 1.8.4. As my project is still under construction, I frequently remove all migration scripts, and rerun makemigrations to generate the initial migration scripts.
I found makemigrations would generate two migration scripts for one of my apps while other apps just have 0001_initial.py. It would be something like:

- 0001_initial.py
- 0002_auto_20150919_1645.py

I checked the content of 0002_auto_20150919_1645.py, it was adding foreign field from the other app's model.
I guess it might be related to the order of creating migrations for apps. So I delete these two migration scripts of this app and then run makemigrations again. Now I have only one migration script for this app.

My questions is: Is there any way I can control the order makemigrations create migrations for apps?

For example, I have two apps, app1 and app2, and app1 depends on app2. Is it possible makemigrations create migration for app2 first, and then app1?

like image 248
Han He Avatar asked Sep 19 '15 09:09

Han He


People also ask

How does Django know which migrations have been applied?

Ensuring that a migration is applied only once requires keeping track of the migrations that have been applied. Django uses a database table called django_migrations . Django automatically creates this table in your database the first time you apply any migrations.

What does Makemigrations do 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.

Why Makemigrations is not working?

This may happen due to the following reasons: You did not add the app in INSTALLED_APPS list in settings.py (You have to add either the app name or the dotted path to the subclass of AppConfig in apps.py in the app folder, depending on the version of django you are using). Refer documentation: INSTALLED_APPS.


1 Answers

You can manually run migrations for an individual app.

./manage.py makemigrations app2
./manage.py makemigrations app1
./manage.py makemigrations # migrate the rest of your apps

You could also squash your existing migrations.

like image 195
Alasdair Avatar answered Oct 02 '22 15:10

Alasdair