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

      Why Non-Native Content Designers Improve Global UX

      July 18, 2025

      DevOps won’t scale without platform engineering and here’s why your teams are still stuck

      July 18, 2025

      This week in AI dev tools: Slack’s enterprise search, Claude Code’s analytics dashboard, and more (July 18, 2025)

      July 18, 2025

      Report: 71% of tech leaders won’t hire devs without AI skills

      July 17, 2025

      Could OpenAI’s rumored browser be a Chrome-killer? Here’s what I’m expecting

      July 18, 2025

      My favorite lens and screen-cleaning kit keeps my tech spotless, and it only costs $8

      July 18, 2025

      AI’s biggest impact on your workforce is still to come – 3 ways to avoid getting left behind

      July 18, 2025

      Remedy offers update on ‘FBC: Firebreak,’ details coming improvements — “We’ve seen many players come into the game and leave within the first hour.”

      July 18, 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 details of TC39’s last meeting

      July 18, 2025
      Recent

      The details of TC39’s last meeting

      July 18, 2025

      Online Examination System using PHP and MySQL

      July 18, 2025

      A tricky, educational quiz: it’s about time..

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

      CAD Sketcher – constraint-based geometry sketcher

      July 18, 2025
      Recent

      CAD Sketcher – constraint-based geometry sketcher

      July 18, 2025

      7 Best Free and Open Source Linux FTP Servers

      July 18, 2025

      Best Free and Open Source Alternatives to Autodesk FBX Review

      July 18, 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

    Artificial Intelligence

    Scaling Up Reinforcement Learning for Traffic Smoothing: A 100-AV Highway Deployment

    July 18, 2025
    Repurposing Protein Folding Models for Generation with Latent Diffusion
    Artificial Intelligence

    Repurposing Protein Folding Models for Generation with Latent Diffusion

    July 18, 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

    Microsoft Patch Tuesday April 2025: One Zero-Day, 11 High-Risk Flaws

    Microsoft Patch Tuesday April 2025: One Zero-Day, 11 High-Risk Flaws

    Development

    CVE-2025-48205 – TYPO3 sr_feuser_register Insecure Direct Object Reference

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-38172 – “Linux EROFS UAF Vulnerability”

    Common Vulnerabilities and Exposures (CVEs)

    DOJ charges 12 more in $263 million crypto fraud takedown where money was hidden in squishmallow stuffed animals

    Development

    Highlights

    CVE-2025-4248 – SourceCodester Simple To-Do List System SQL Injection

    May 4, 2025

    CVE ID : CVE-2025-4248

    Published : May 4, 2025, 6:15 a.m. | 5 hours, 12 minutes ago

    Description : A vulnerability has been found in SourceCodester Simple To-Do List System 1.0 and classified as critical. Affected by this vulnerability is an unknown functionality of the file /complete_task.php. The manipulation of the argument ID leads to sql injection. The attack can be launched remotely. The exploit has been disclosed to the public and may be used.

    Severity: 6.3 | MEDIUM

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    CVE-2025-53836 – XWiki Rendering Macro Execution Bypass

    July 15, 2025

    Chrome Use-After-Free Vulnerabilities Exploited in the Wild

    April 25, 2025

    CVE-2025-49296 – Mikado-Themes GrandPrix Path Traversal PHP Local File Inclusion Vulnerability

    June 9, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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