Home Interests Django

How to Rerun a Django Migration

howchoo
howchoo   (467)
August 25, 2023
4 minutes

Share

Interests
Posted in these interests:
django • 3 guides
python • 18 guides

By default, Django migrations are run only once. But sometimes we need to rerun a Django migration, especially when testing custom migrations during development. This guide will show you how to do just that.

1 – Fake back to the migration immediately before the one you want to rerun

First, imagine the migration history for myapp looks like this:

$ ./manage.py showmigrations myapp
myapp
...
[x] 0004_the_migration_right_before
[x] 0005_the_migration_i_want_to_rerun
[x] 0006_a_migration_i_dont_care_about
...
[x] 0010_the_latest_migration

It’s wise to visualize the migration history before messing with migrations. When we’re finished with this process we can ensure the final migration state matches the original state.

So the latest migration is 0010_the_latest_migration and we want to rerun 0005_the_migration_i_want_to_rerun.

We’ll use the --fake flag to “fake” back to the migration before the one we want to rerun. We’ll also need to specify the target app for the migration, which in this case is myapp.

./mange.py migrate --fake myapp 0004_the_migration_right_before

Keep in mind, this is an advanced feature that modifies the migration state. It can cause issues that require manual recovery.

2 – Rerun the target migration

Now we can run only our target migration:

./mange.py migrate myapp 0005_the_migration_i_want_to_rerun

3 – Fake back to the latest migration

Now, in order to restore the original migration state, we need to fake back to the latest migration:

./mange.py migrate --fake myapp 0010_the_latest_migration

We can confirm our migration history looks correct by running the following, and comparing to the original state:

./manage.py showmigrations myapp
myapp
...
[x] 0004_the_migration_right_before
[x] 0005_the_migration_i_want_to_rerun
[x] 0006_a_migration_i_dont_care_about
...
[x] 0010_the_latest_migration
NEXT UP

Class Vs. Instance Variables in Python 3

howchoo
howchoo   (467)
November 22, 2023

When learning object oriented programming in Python, there can be a few gotchas when it comes to distinguishing between class and instance variables. In this guide I’ll explain the difference between class and instance variables and provide examples demonstrating various use cases. 1 – Class vs. instance variables First, a quick review if you’re new

Continue Reading

howchoo

 467 guides

Introducing Howchoo, an enigmatic author whose unique pen name reflects their boundless curiosity and limitless creativity. Mysterious and multifaceted, Howchoo has emerged as a captivating storyteller, leaving readers mesmerized by the uncharted realms they craft with their words. With an insatiable appetite for knowledge and a love for exploration, Howchoo's writing transcends conventional genres, blurring the lines between fantasy, science fiction, and the surreal. Their narratives are a kaleidoscope of ideas, weaving together intricate plots, unforgettable characters, and thought-provoking themes that challenge the boundaries of imagination.

Discover interesting things!

Explore Howchoo's most popular interests.

Explore