Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Jonathan Huppi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jonathan Huppi
Front End Web Development Techdegree Graduate 42,048 Points

django-admin migrate [app_label] [migration_name]

I've been working on a Django project for a while now, and have realized that my initial model structure isn't ideal. I'd rather not flush my database and delete the tables, because it takes a while and its easy to make errors. During my search for a quicker/safer solution I came across this command:

django-admin migrate [app_label] [migration_name]

According to the docs, '<app_label> <migrationname>: Brings the database schema to a state where the named migration is applied, but no later migrations in the same app are applied. This may involve unapplying migrations if you have previously migrated past the named migration. Use the name zero to unapply all migrations for an app.'

I tried using 'zero' to unapply all migrations, but when I went to make a new migration Django didn't override the initial migration. The new migration was simply added in order to the previous migrations.

Am I missing something here?

Benjamin Lange
Benjamin Lange
16,178 Points

Once you update your model and are ready to apply a migration, there are two commands to run.

  1. python manage.py makemigrations [app_name]
  2. python manage.py migrate [app_name]

If you need to rollback a migration, run the following to get the name of each migration that you have so far.

  1. python manage.py showmigrations [app_name]
  2. Then, run python manage.py migrate [app_name] [migration_name] to rollback to a previous migration point.
  3. Then, you can safely delete the failed migration file from your [app_name]/migrations folder.