Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      CodeSOD: A Unique Way to Primary Key

      July 22, 2025

      BrowserStack launches Figma plugin for detecting accessibility issues in design phase

      July 22, 2025

      Parasoft brings agentic AI to service virtualization in latest release

      July 22, 2025

      Node.js vs. Python for Backend: 7 Reasons C-Level Leaders Choose Node.js Talent

      July 21, 2025

      The best CRM software with email marketing in 2025: Expert tested and reviewed

      July 22, 2025

      This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

      July 22, 2025

      I’m a wearables editor and here are the 7 Pixel Watch 4 rumors I’m most curious about

      July 22, 2025

      8 ways I quickly leveled up my Linux skills – and you can too

      July 22, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025
      Recent

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025

      Zero Trust & Cybersecurity Mesh: Your Org’s Survival Guide

      July 22, 2025

      Execute Ping Commands and Get Back Structured Data in PHP

      July 22, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025
      Recent

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025

      “I don’t think I changed his mind” — NVIDIA CEO comments on H20 AI GPU sales resuming in China following a meeting with President Trump

      July 22, 2025

      Galaxy Z Fold 7 review: Six years later — Samsung finally cracks the foldable code

      July 22, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»How to Revert a Migration in Django

    How to Revert a Migration in Django

    July 16, 2025

    So, you’re working with Django, you’ve run a migration, and now something’s broken. Maybe you added a field that shouldn’t be there.

    Maybe you renamed a model, and suddenly your database is a mess. Or maybe you’re just experimenting and want to roll things back.

    That’s where reverting migrations comes in.

    Knowing how to undo a migration in Django is just as important as knowing how to make one. It’s not about being perfect – it’s about being able to fix mistakes fast, without panic. I’ve been there.

    A single migration can break everything if it goes wrong. But the good news is, Django gives you tools to take a step back safely.

    Let me walk you through how this works, using plain language and clear steps.

    Table of Contents

    • What Exactly Is a Migration in Django?

    • How to Revert a Migration in Django

      • Case 1: Undo a Migration That’s Already Been Applied

      • Case 2: Undo a Migration You Haven’t Applied Yet

    • Special Case: Reverting All Migrations (Reset Everything)

    • Example Scenario: Fixing a Broken Migration

    • Common Pitfalls (And How to Avoid Them)

    • FAQs

      • Can I revert more than one migration at a time?

      • How do I know which migration names to use?

      • Is reverting migrations safe?

    • Further Resources

    • Conclusion

    What Exactly Is a Migration in Django?

    Before we can talk about undoing a migration, let’s make sure we’re on the same page.

    A migration in Django is a record of changes to your database. It tracks what models you’ve added or changed, and applies those changes to your actual database using SQL (behind the scenes).

    You usually create a migration with this command:

    python manage.py makemigrations
    

    And apply it like this:

    python manage.py migrate
    

    That’s when Django updates your database tables to match your models.

    Now, what if you want to undo that last step?

    How to Revert a Migration in Django

    Alright, here’s the main part. Let’s say you just ran a migration and want to undo it. There are two situations:

    1. You applied the migration already and want to reverse it

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

    Let’s handle both.

    Case 1: Undo a Migration That’s Already Been Applied

    If you’ve already run python manage.py migrate, Django has changed your database.

    To reverse that migration, use this:

    python manage.py migrate your_app_name migration_name_before
    

    Let me break that down:

    • your_app_name is the name of your Django app (like blog, users, or store)

    • migration_name_before is the name of the migration before the one you want to undo

    Let’s go through an example.

    Say you have these migrations for an app called store:

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

    If you want to undo the 0003_change_price_field.py migration, you’d run:

    python manage.py migrate store 0002
    

    That tells Django to roll back to migration 0002, effectively undoing everything in 0003.

    Once that’s done, you’ll see output like:

    Operations to reverse:
       - Alter field price on product
    

    And your database is back the way it was before 0003.

    Case 2: Undo a Migration You Haven’t Applied Yet

    Maybe you ran makemigrations, but not migrate. So you just created the migration file and haven’t actually touched the database yet.

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

    Just go into your app’s migrations/ folder, and delete the unwanted migration file (for example: 0003_change_price_field.py).

    Then you can re-run makemigrations with the correct changes.

    Quick tip: Don’t delete __init__.py or the 0001_initial.py file unless you know what you’re doing. That first one is usually required.

    Special Case: Reverting All Migrations (Reset Everything)

    Sometimes you just want to wipe all the migrations and start over.

    This is common when you’re still in development, and your database structure is messy.

    Here’s how I usually do it:

    1. Delete the migration files inside the migrations/ folder of your app (except for __init__.py)

    2. Drop the database or just clear the tables if you’re using SQLite or a test DB

    3. Run:

    python manage.py makemigrations
    python manage.py migrate
    

    If you’re using SQLite, you can also just delete the .sqlite3 file and start fresh.

    For PostgreSQL or MySQL, you’ll need to drop and recreate the database, or reset it using a tool like pgAdmin or DBeaver.

    Example Scenario: Fixing a Broken Migration

    Let’s 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 a typo:

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

    Oops. Django lets you do this:

    python manage.py makemigrations store
    python manage.py migrate
    

    Then it breaks.

    You can fix this by reverting:

    python manage.py migrate store 0001
    

    Then fix the typo in your model and run:

    python manage.py makemigrations
    python manage.py migrate
    

    Back on track!

    Common Pitfalls (And How to Avoid Them)

    • Don’t just delete a migration without reversing it first. This can confuse Django.

    • Always check which migrations are applied using:

    python manage.py showmigrations
    
    • Keep backups of your database, especially in production. Use tools like pg_dump or mysqldump if needed.

    • Don’t reset migrations in a live app unless you absolutely must. It can mess up production data.

    FAQs

    Can I revert more than one migration at a time?

    Yes! You just migrate back to the point before the migrations you want to undo.

    Example:

    You’ve applied:

    [X] 0001_initial  
    [X] 0002_add_price_to_product  
    [X] 0003_change_price_field  
    [X] 0004_add_discount_field
    

    To undo both 0004 and 0003, run:

    python manage.py migrate store 0002
    

    This rolls back both 0004 and 0003, leaving only 0001 and 0002 applied.

    How do I know which migration names to use?

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

     [X] 0001_initial
     [X] 0002_add_price_to_product
     [X] 0003_change_price_field
    

    The [X] shows applied migrations. To undo 0003, migrate back to 0002.

    Is reverting migrations safe?

    It is, as long as you haven’t made changes to data that depend on the migration. Always test in development before trying in production.

    Conclusion

    Reverting migrations in Django isn’t scary once you get the hang of it. It’s like using undo in a Word document – you just need to know how far back to go.

    So now that you know how to revert a migration in Django, what’s the trickiest migration issue you’ve run into—and how did you fix it?

    Shoot me a message – I’d love to hear your story.

    Further Resources

    • Official Django Migration Docs

    • Django Migrations Primer by RealPython

    • Common Django Mistakes and How to Avoid Them

    Source: freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleCan AI really code? Study maps the roadblocks to autonomous software engineering
    Next Article How to Protect Your GitHub Repos Against Malicious Clones

    Related Posts

    Development

    GPT-5 is Coming: Revolutionizing Software Testing

    July 22, 2025
    Development

    Win the Accessibility Game: Combining AI with Human Judgment

    July 22, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Redis Reintroduces Open-Source AGPL Alongside SSPL Licensing

    Security

    The Game-Changing Role of App Modernization in the Finance Industry

    Web Development

    CVE-2025-37817 – Linux kernel Double Free in Chameleon Driver

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-40775 – BIND DNS Invalid TSIG Algorithm Field Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    Apple Appeals €500M EU DMA Fine: Challenges “Unprecedented” Ruling on App Store Policies

    July 7, 2025

    Apple Appeals €500M EU DMA Fine: Challenges “Unprecedented” Ruling on App Store Policies

    In response to the European Union’s €500 million fine levied for alleged violations of the Digital Markets Act (DMA), Apple has formally filed an appeal with the General Court of the European Union, d …
    Read more

    Published Date:
    Jul 08, 2025 (55 minutes ago)

    Vulnerabilities has been mentioned in this article.

    CVE-2023-23529

    WhatsApp privacy is ‘broken,’ reveals proof-of-concept hack

    April 9, 2025

    CVE-2024-52874 – Infoblox NETMRI SQL Injection

    May 22, 2025

    Gemini for kids is rolling out, and it comes with 3 warnings from Google

    May 1, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.