Skip to content

Instantly share code, notes, and snippets.

@RamsesMartinez
Last active August 3, 2018 04:53
Show Gist options
  • Save RamsesMartinez/6ef42bb9d11399e03ee215d107a70499 to your computer and use it in GitHub Desktop.
Save RamsesMartinez/6ef42bb9d11399e03ee215d107a70499 to your computer and use it in GitHub Desktop.
How to create a initial_migrations from an existing schema

Here are the steps (for whoever runs into this problem):

  1. Empty the django_migrations: table: delete from django_migrations;
  2. For every app, delete its migrations folder: rm -rf <app>/migrations/
    find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
    find . -path "*/migrations/*.pyc"  -delete
    
  3. Reset the migrations for the "built-in" apps: python manage.py migrate --fake
  4. For each app run: python manage.py makemigrations <app>. Take care of dependencies (models with ForeignKey's should run after their parent model).
  5. Finally: python manage.py migrate --fake-initial

After that I ran the last command without the --fake-initial flag, just to make sure.

Now everything works and I can use the migrations system normally.

Update for Django >= 1.9 users:

I had this scenario again with a Django 1.9.4, and step 5 failed. All we have to do is replace --fake-initial with --fake to make it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment