3

I have an existing database filled with a bunch of data. I want to migrate to Django so I went ahead and created the necessary models with python manage.py inspectdb. I haven't changed anything besides removed the managed = False since I want Django to manage the tables (mistake perhaps for initial migration?).

So now that the models are ready, how can I generate the first migration file so that I can start changing the fields to generate additional migrations (renaming a field here and there). I understand python manage.py migrate will handle the creation of Django-specific models but does not actually create any migration files? Some sources indicate the first migration file should be run with --fake so it's not applied. Will the next migration files remember to run the first one as fake and only apply the next ones?

mart1n
  • 5,969
  • 5
  • 46
  • 83

1 Answers1

3

You want makemigrations to create the migrations. The migrate command applies migrations, it does not create them.

You can use the --fake-initial option so that Django does not try to create the tables that already exist. Note that --fake and --fake-initial are two different commands.

When you run migrate, the django_migrations table is updated to store the currently applied migrations. The migration files themselves are not changed. The --fake command updates the django_migrations table without running the migration. That means that if you use it incorrectly your database and django_migrations table can get out of sync, which can be difficult to fix.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • So for a new project, the process should be: `makemigrations `, `migrate --fake`, and then `migrate` to create the Django-specific tables? After that I can alter the models, run `makemigrations ; migrate` again, correct? – mart1n Apr 17 '18 at 13:54
  • You've missed my suggestion to use `--fake-initial`, but that would work, as long as you run `migrate --fake` before you making alterations and running `makemigrations` again. – Alasdair Apr 17 '18 at 13:58
  • Oh, I see, I thought `--fake` was a shorthand for `--fake-initial` but it actually creates the Django-specific tables so no need to run an extra migrate. Thank you! It seems to make a bit more sense now. – mart1n Apr 17 '18 at 14:03