1

I have a question about the order in which tables are created in a migration. As there is a ForeignKey in model B to connect to model A, I create models with order of A, B and C in models.py. Then:

python manage.py makemigrations app

There is the migration file generated to create all the models, but the order is:

- Create model B
- Create model C
- Create model A
- Add field a_name to b.

As the order in models.py really matters, but why doesn't makemigrations follow the given order?

e4c5
  • 52,766
  • 11
  • 101
  • 134
zhihong
  • 1,808
  • 2
  • 24
  • 34
  • 1
    Why does the order matter in your models.py? Is that still relevant in the migration files? – knbk Sep 14 '16 at 11:04
  • @knbk, as I use the ForeignKey in the model B, so model A should be created before B. Though I read some doc that app. A can be used in model B to avoid the order issue, but here I keep to create the model in the right order in models.py – zhihong Sep 14 '16 at 12:43

1 Answers1

2

The order in which you place your models in your models.py matters if and only if one of them references another as a ForeignKey. In such a situation the order is important and you will find the the migration does preserve the order.

What's really important is not what shows up when you do manage.py makemigrations but what happens when you do manage.py migrate there django usually figures out the correct order. If at anytime you feel that you want to control the order in which tables are created, you are free to edit the migration file (even though this is not really needed)

e4c5
  • 52,766
  • 11
  • 101
  • 134
  • yes, I do have ForeignKey used, that is why I control the model order in models.py. But the auto - generated migration file dose not follow the order. So that is a normal case, right? For me, it is just a little bit confusing. I expect to only see "-create the model", but not like "-create the model -add field to the model", this is like create and modify a table. – zhihong Sep 14 '16 at 12:55
  • Ah, I see, I know that the migrations can be modified, but as you said, will not do that if it is really needed. Thanks very much for the explanation. – zhihong Sep 14 '16 at 13:39
  • Hi, as I also need to control the order of the columns when created the table in the database, but the manage.py makemigrations dose not reserve the order when a column is a ForeignKey column, so I modified the migration file. I really hope the migration file has the models and the columns follow the order from the models.py, but seems it dose not. – zhihong Sep 21 '16 at 10:08
  • that really is a completely different question and nothing to do with this one shall we post another question for it? – e4c5 Sep 21 '16 at 10:13
  • Hi, I did not post another question yet, as I manually modified the migration file as a work around. I think the question here is related to that question. As I mentioned that only the ForeignKey columns did not generated as the order of the models.py. And I already accept your answers as correct last week. – zhihong Sep 26 '16 at 07:39
  • Hi, sorry, I did mistake vote up and correct, still in a Monday sleeping mode :) – zhihong Sep 26 '16 at 07:59