How to flip the migration in Jingo

by SkillAiNest

So, you are working with Jiango, you have migrated, and now something is broken. You may have added a field that should not be there.

You may have changed the name of a model, and suddenly your database is dirt. Or maybe you are just experimenting and wanting to back things.

From this place the migration comes from.

It is as important to know how to eliminate migration in Jiango as it is to know how to make it. It’s not about being perfect – it’s about being able to fix mistakes, without panic, without panic. I am there.

If the same migration goes wrong, then everything can be broken. But the good news is, Jiango gives you tools tools to safely take you a step back.

Using simple language and clear steps, let me work on how I work for you.

The table of content

Migration in Jiango is all right?

Before we can talk about eliminating migration, let’s make sure we are on the same page.

The migration to Jiango is a record of changes in your database. This shows which models you have added or replaced, and apply these changes using SQL (behind the screen) to your original database.

You usually create migration with this command:

python manage.py makemigrations

And apply it like this:

python manage.py migrate

At the same time, Jiango updates your database table to meet your models.

Now, if you want to ban this last step, what will happen?

How to flip the migration in Jingo

Well, here’s an important part. We say that you have just migrated and want to revoke it. There are two situations:

  1. You have already applied migration and want to turn it up

  2. You haven’t applied it yet and just want to delete it

Let’s handle both.

Case 1: Retrieve the migration that has already been implemented

If you have already been running python manage.py migrateJiango has changed your database.

To overturn this migration, use it:

python manage.py migrate your_app_name migration_name_before

Let me break it:

  • your_app_name Your Jiango app is the name (like blogFor, for, for,. usersOr storeJes

  • migration_name_before The name of the migration is Ago Which you want to ban

Let’s go through an example.

Says you have this migration for an app store:

0001_initial.py  
0002_add_price_to_product.py  
0003_change_price_field.py

If you want to be banned 0003_change_price_field.py Emigrate, you’ll walk:

python manage.py migrate store 0002

Who tells Jiango to go back to migration 0002Effectively disobeying everything 0003.

Once it happens, you will see output like this:

Operations to reverse:
   - Alter field price on product

And your database has returned like before 0003.

Case 2: Retrieve the migration you have not yet applied

Maybe you ran away makemigrationsBut not migrate. So you have just made the migration file and have not yet touched the database yet.

In this case, you can safely delete the migration file.

Just go to your app migrations/ Folder, and delete unwanted transfer file (eg: 0003_change_price_field.py,

Then you can run again makemigrations With the right changes.

Instant gesture: Don’t delete __init__.py Or 0001_initial.py Unless you know what you are doing. It is usually needed.

Special Case: Changing all migration (reset everything)

Sometimes you just want to wipe all migration and start.

This is common when you are still in development, and your database structure is dirty.

Here I usually do this:

  1. Delete migrants files In migrations/ Your app’s folder (except that __init__.pyJes

  2. Drop the database Or if you are using sqlite or test DB just clean the tables

  3. Drive:

python manage.py makemigrations
python manage.py migrate

If you are using sqlite, you can only delete .sqlite3 File and start fresh.

Postgrass QL or SQL LO you, you will need to leave and regenerate the database, or to reset using a tool pgAdmin Or DBeaver.

Example scenario: fixing broken migration

We say you added a new field to a model:

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=6, decimal_places=2, default=0.00)

You made the type of:

price = models.DecimalField(max_digits=6, decimal_places=2, default='free')

Ofo Jiango lets you do this:

python manage.py makemigrations store
python manage.py migrate

Then it breaks.

You can resolve it by turning it out:

python manage.py migrate store 0001

Then fix the typewy in your model and drive:

python manage.py makemigrations
python manage.py migrate

Back to track!

Normal defects (and how to avoid them)

python manage.py showmigrations
  • Keep your database backupEspecially in production. Use tolls like pg_dump Or mysqldump If needed.

  • Do not reset the migration directly to the app As long as you are essential. This can disrupt the production data.

Normal questionnaire

Can I migrate more than one at a time?

Yes! You just migrate back to this point Ago The migration you want to ban.

Example:

You have applied:

(X) 0001_initial  
(X) 0002_add_price_to_product  
(X) 0003_change_price_field  
(X) 0004_add_discount_field

To ban both of them 0004 And 0003Drive:

python manage.py migrate store 0002

It returns both 0004 and 0003, which only applies 0001 and 0002.

How can I know what names of migration use?

Drive python manage.py showmigrations And you’ll see a list:

 (X) 0001_initial
 (X) 0002_add_price_to_product
 (X) 0003_change_price_field

(X) Apply shows migration. To be banned 0003Migrate back 0002.

Is the transfer change safe?

Unless you have made changes to the data that depend on the migration. Always test the growth before trying in production.

Conclusion

Once you get the execution of it, it is not terrifying to return to Jiango. This is equivalent to using a banned in a word document – you just need to know how far go back.

So now that you know how to return to Jiangu, what is the most difficult problem of migration in which you are running – and how did you fix it?

Shoot me a Massage – I would love to hear your story.

More resources

You may also like

Leave a Comment

At Skillainest, we believe the future belongs to those who embrace AI, upgrade their skills, and stay ahead of the curve.

Get latest news

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

@2025 Skillainest.Designed and Developed by Pro