Skip to content

Instantly share code, notes, and snippets.

@jim-clark
Last active July 14, 2025 13:06

Revisions

  1. jim-clark revised this gist Jul 14, 2025. 1 changed file with 13 additions and 11 deletions.
    24 changes: 13 additions & 11 deletions rollback-django-migrations.md
    Original file line number Diff line number Diff line change
    @@ -52,19 +52,21 @@ If you want to unapply **all** migrations for an app - and losing all data creat
    python3 manage.py migrate main_app zero
    ```

    ## Editing Migration(s)
    ## Deleting and Replacing Migration(s)

    Once a migration has been unapplied, it's possible to carefully edit the migration file, for example, to correct a field type.
    After editing, the migration(s) may be ran again using the familiar migrate command:
    Once migrations has been unapplied, it's safe to:
    1. Delete ALL of the unapplied deletions.
    2. Delete the `__pycache__` folder that's inside of the `migrations` folder.
    3. Edit the models as desired.
    4. Makemigrations and migrate.

    ```
    python3 manage.py migrate
    ```

    ## Deletion Migration(s)
    ## If All Else Fails

    It's also possible to delete the unapplied migration files, however, all unapplied migration files should be removed otherwise errors may result.
    If errors are still being encountered, it may be necessary to delete the database...

    After deleting, it's also **important** to delete the `__pycache__` folder that's inside of the `migrations` folder.
    > 👀 All existing data and users will be deleted.
    With the migration file(s) deleted, you may make the necessary changes to your model(s) and makemigrations/migrate afterwards.
    1. Delete the database in Neon.
    2. Recreate your database in Neon.
    3. Delete everything that's inside of the `main_app/migrations` folder (including the `__pycache__` folder) EXCEPT for the `__init__.py` file.
    4. Makemigrations and migrate.
  2. jim-clark revised this gist Feb 7, 2025. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions rollback-django-migrations.md
    Original file line number Diff line number Diff line change
    @@ -23,23 +23,23 @@ main_app

    ## Rolling Back to a Specified Migration

    Let's say that you wanted to modify, e.g., rename/add/remove or change the type of field, in the Photo model. To do so requires the migration created by adding the `Photo` model be rolled back (unapplied) using the following command:
    Let's say that you wanted to modify, e.g., rename/add/remove or change the type of field, in the Toy model. To do so requires the migration created by adding the `Toy` model be rolled back (unapplied) using the following command:

    > 👀 Warning: Rolling back migrations can result in the loss of data without warning. For example, the following will result in the removal of the entire toys table from the database!
    ```shell
    python3 manage.py migrate main_app 0003
    python3 manage.py migrate main_app 0002
    ```

    > 👀 Note that an `IrreversibleError` will occur if it's not possible to roll back a migration due to database integrity reasons.
    The above command will unapply **all** migrations **after** the migration number 0003 (0003_toy_alter_feeding_options_alter_feeding_date) and running the same showmigrations command will confirm that those migrations have been unapplied:
    The above command will unapply **all** migrations **after** the migration number 0002 (0002_feeding.py). Running the same showmigrations command will confirm that those migrations have been unapplied:

    ```
    main_app
    [X] 0001_initial
    [X] 0002_feeding
    [X] 0003_toy_alter_feeding_options_alter_feeding_date
    [ ] 0003_toy_alter_feeding_options_alter_feeding_date
    [ ] 0004_cat_toys
    [ ] 0005_cat_user
    ```
  3. jim-clark revised this gist Sep 23, 2024. 1 changed file with 6 additions and 8 deletions.
    14 changes: 6 additions & 8 deletions rollback-django-migrations.md
    Original file line number Diff line number Diff line change
    @@ -18,32 +18,30 @@ main_app
    [X] 0002_feeding
    [X] 0003_toy_alter_feeding_options_alter_feeding_date
    [X] 0004_cat_toys
    [X] 0005_photo
    [X] 0006_cat_user
    [X] 0005_cat_user
    ```

    ## Rolling Back to a Specified Migration

    Let's say that you wanted to modify, e.g., rename/add/remove or change the type of field, in the Photo model. To do so requires the migration created by adding the `Photo` model be rolled back (unapplied) using the following command:

    > 👀 Warning: Rolling back migrations can result in the loss of data without warning. For example, the following will result in the removal of the entire photos table from the database!
    > 👀 Warning: Rolling back migrations can result in the loss of data without warning. For example, the following will result in the removal of the entire toys table from the database!
    ```shell
    python3 manage.py migrate main_app 0004
    python3 manage.py migrate main_app 0003
    ```

    > 👀 Note that an `IrreversibleError` will occur if it's not possible to roll back a migration due to database integrity reasons.
    The above command will unapply **all** migrations **after** the migration number 0004 (0004_cat_toys) and running the same showmigrations command will confirm that those migrations have been unapplied:
    The above command will unapply **all** migrations **after** the migration number 0003 (0003_toy_alter_feeding_options_alter_feeding_date) and running the same showmigrations command will confirm that those migrations have been unapplied:

    ```
    main_app
    [X] 0001_initial
    [X] 0002_feeding
    [X] 0003_toy_alter_feeding_options_alter_feeding_date
    [X] 0004_cat_toys
    [ ] 0005_photo
    [ ] 0006_cat_user
    [ ] 0004_cat_toys
    [ ] 0005_cat_user
    ```

    ## Rolling Back All Migrations
  4. jim-clark created this gist Sep 25, 2023.
    72 changes: 72 additions & 0 deletions rollback-django-migrations.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    # Rolling Back Django Migrations

    Django migrations can be unapplied to a database in the reverse order that they were applied using the following commands...

    ## Listing the Migrations for a Specific Django App

    Run the following to list the existing migrations for the `main_app` Django app:

    ```shell
    python3 manage.py showmigrations main_app
    ```

    ...which will output something like this for the catcollector:

    ```
    main_app
    [X] 0001_initial
    [X] 0002_feeding
    [X] 0003_toy_alter_feeding_options_alter_feeding_date
    [X] 0004_cat_toys
    [X] 0005_photo
    [X] 0006_cat_user
    ```

    ## Rolling Back to a Specified Migration

    Let's say that you wanted to modify, e.g., rename/add/remove or change the type of field, in the Photo model. To do so requires the migration created by adding the `Photo` model be rolled back (unapplied) using the following command:

    > 👀 Warning: Rolling back migrations can result in the loss of data without warning. For example, the following will result in the removal of the entire photos table from the database!
    ```shell
    python3 manage.py migrate main_app 0004
    ```

    > 👀 Note that an `IrreversibleError` will occur if it's not possible to roll back a migration due to database integrity reasons.
    The above command will unapply **all** migrations **after** the migration number 0004 (0004_cat_toys) and running the same showmigrations command will confirm that those migrations have been unapplied:

    ```
    main_app
    [X] 0001_initial
    [X] 0002_feeding
    [X] 0003_toy_alter_feeding_options_alter_feeding_date
    [X] 0004_cat_toys
    [ ] 0005_photo
    [ ] 0006_cat_user
    ```

    ## Rolling Back All Migrations

    If you want to unapply **all** migrations for an app - and losing all data created thus far, here's the command:

    ```shell
    python3 manage.py migrate main_app zero
    ```

    ## Editing Migration(s)

    Once a migration has been unapplied, it's possible to carefully edit the migration file, for example, to correct a field type.
    After editing, the migration(s) may be ran again using the familiar migrate command:

    ```
    python3 manage.py migrate
    ```

    ## Deletion Migration(s)

    It's also possible to delete the unapplied migration files, however, all unapplied migration files should be removed otherwise errors may result.

    After deleting, it's also **important** to delete the `__pycache__` folder that's inside of the `migrations` folder.

    With the migration file(s) deleted, you may make the necessary changes to your model(s) and makemigrations/migrate afterwards.