How do I rollback migrations in Django?

I’d like someone to help address one of my doubts in Django migration and DB rollback. Is it possible to rollback the DB migration in Django? How do I rollback migrations in Django? Or, it’s impossible to roll back to an earlier point? @Crowdbotics_Dan @anand and @andresmachado

1 Like

The better approach is to roll forward. You can retrieve the state of the model schema at a prior time inside your Git repo, then merge that into your current repo as a new commit.

Let’s assume that you’re using the Crowdbotics Model Builder to build your models. Each time you save your models, Crowdbotics carries out the following steps:

  • Writes the code for your current models, APIs, and admin views (into the various,, and files in your repo, respectively)
  • Checks for changes between your current schema and the last schema you saved
  • Writes a database migration to safely transform your last schema into the current one
  • Writes a pull request to commit the new code and migration into your repo, replacing the old one
  • Finally, when you push “deploy”, Crowdbotics will check the state of your database, and apply all the migrations in your repo for you that haven’t been applied yet.

That means that you have a list of commits in your repo corresponding to each of the various changes you’ve made. As a neat byproduct, you can access any of the prior states of your models by going back to your committed git history, which gives us a way to revert our database schema!

Here’s what to do:

  • Click “view code” in “settings” to access your Git repo.
  • Browse the commit history to swap back to the desired model state that you want, and check it out locally as a new branch
  • Commit the model file back to the top of your repo, and push it back to the remote
  • Click “deploy”. Crowdbotics will deploy your app, and automagically generate and commit the new migration file necessary to get back into the correct database schema.

That’s it! You can check the schema displayed in Model Builder anytime to make sure you’re on the desired schema.

1 Like