• Shopping Cart Shopping Cart
    0Shopping Cart
London App Developer
  • Courses
  • Consulting
  • Tutorials
  • About
  • Click to open the search input field Click to open the search input field Search
  • Menu Menu
migrate versus makemigrations with a thinking emoji
Django

What’s the difference between the migrate and makemigrations command in Django?

When writing applications for relational databases such as MySQL or PostgreSQL, there are two things to keep in mine:

  • The first is the structure of the database itself. This includes things such as the tables, fields and relationships between tables.
  • The second is how the objects in yout application map to data in your database. This involves writing code for mapping objects in our application to rows in the database and vica versa.

These two components need to work together, because if the structure of your database tables deviates from your objects, then you’ll get errors in your code.

Historically, this had to be done manually by writing SQL statements to create tables and their structures in your database. For example, if your application had to handle “products” you might write some SQL to create a table called “Product” with a number of fields used for storing the data. 

Then you would write some code that made assumptions about this table (such as the name, fields, types, maximum character lengths, etc…). If you needed to make a change to the data (such as adding a new field), you would need to do this in both your app code and the database by writing SQL to modify the table structure.

The problem with this approach, is it gets difficult to manage quickly. As your site grows and you make more and more changes, the risk of errors grows which results in bugs, slow development, instabaility, data loss and so on…

Django has a brilliant solution to this problem, called the Object Relational Mapper (ORM).

The idea is that instead of managing your database manually, Django provides a set of tools which does it on your behalf.

These tools include the following:

  1. Database models to help you define the structure of your data in your code, and map them to objects in your database.
  2. The makemigrations command which auto-generates migration files that contain instructions for making changes to your database.
  3. The migrate command which run the instructions defined in your migrations.

This is where the migrate and makemigrations command comes in…

When you run makemigrations, Django will identify any changes you’ve made to your database models. If it sees a change, it will generate a migration file which contains the modifications required to make your database structure consistent with the model change.

Then, when you run the migrate command, it will look at these migrations and use them to make any changes required to your database.

So the difference between makemigrations and migrate is this:

  • makemigrations auto generates migration files containing changes that need to be applied to the database, but doesn’t actually change anyhting in your database.
  • migrate will make the actual modifications to your database, based on the migration files.
Share this entry
  • Share on Facebook
  • Share on X
  • Share on WhatsApp
  • Share on Pinterest
  • Share on LinkedIn
  • Share on Tumblr
  • Share on Vk
  • Share on Reddit
  • Share by Mail
https://i0.wp.com/londonappdeveloper.com/wp-content/uploads/2021/03/Artboard-1-2.png?fit=574%2C446&ssl=1 446 574 mark https://londonappdeveloper.com/wp-content/uploads/2018/12/lon_website_logo-300x110.png mark2021-03-31 13:55:212021-04-04 16:00:31What’s the difference between the migrate and makemigrations command in Django?
2 replies
  1. VIKAS GUPTA
    VIKAS GUPTA says:
    October 22, 2021 at 4:46 am

    Useful content dear.

    Reply
  2. ashkan
    ashkan says:
    March 27, 2022 at 10:22 am

    That was great tutorial …
    thx

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pages

  • About
  • Consulting
  • Home
  • Tutorials

Categories

  • AI
  • Android
  • Android Studio
  • Angular
  • Aptana Studio
  • AWS
  • Bootstrap
  • Career advice
  • Celery
  • Databases
  • Deployment
  • DevOps
  • Django
  • Django REST Framework
  • Docker
  • Eclipse
  • Getting Help
  • Git
  • Git Bash
  • Git Flow
  • GitHub Actions
  • Google App Engine
  • IDE
  • Ionic Framework
  • iOS
  • JavaScript
  • Job Search
  • Links
  • Linux
  • Mac OS X
  • macOS
  • Node.JS
  • OpenAI
  • PGAdmin
  • Postgres
  • Productivity
  • Python
  • React
  • Salt Stack
  • Stack Overflow
  • Tools
  • Travis-CI
  • Tutorials
  • Ubuntu
  • Uncategorized
  • Vagrant
  • VSCode
  • WebSockets
  • Windows 10
  • WordPress

London App Developer Ltd,

71-75 Shelton Street, Covent Garden, London, United Kingdom, WC2H 9JQ

Company registration number: 09718346

© London App Developer - London App Developer Ltd, Company Registration Number: 09718346, VAT: GB224061842
  • Link to X
  • Link to Youtube
  • Link to Facebook
  • Link to Reddit
Link to: How do you debug a Django application that is running in Docker? Link to: How do you debug a Django application that is running in Docker? How do you debug a Django application that is running in Docker?django and docker logos with a picture of a spider Link to: Installing Python on macOS using PyEnv for beginners Link to: Installing Python on macOS using PyEnv for beginners blog title with the python logoInstalling Python on macOS using PyEnv for beginners
Scroll to top Scroll to top Scroll to top

This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies.

OKLearn more

Cookie and Privacy Settings



How we use cookies

We may request cookies to be set on your device. We use cookies to let us know when you visit our websites, how you interact with us, to enrich your user experience, and to customize your relationship with our website.

Click on the different category headings to find out more. You can also change some of your preferences. Note that blocking some types of cookies may impact your experience on our websites and the services we are able to offer.

Essential Website Cookies

These cookies are strictly necessary to provide you with services available through our website and to use some of its features.

Because these cookies are strictly necessary to deliver the website, refusing them will have impact how our site functions. You always can block or delete cookies by changing your browser settings and force blocking all cookies on this website. But this will always prompt you to accept/refuse cookies when revisiting our site.

We fully respect if you want to refuse cookies but to avoid asking you again and again kindly allow us to store a cookie for that. You are free to opt out any time or opt in for other cookies to get a better experience. If you refuse cookies we will remove all set cookies in our domain.

We provide you with a list of stored cookies on your computer in our domain so you can check what we stored. Due to security reasons we are not able to show or modify cookies from other domains. You can check these in your browser security settings.

Google Analytics Cookies

These cookies collect information that is used either in aggregate form to help us understand how our website is being used or how effective our marketing campaigns are, or to help us customize our website and application for you in order to enhance your experience.

If you do not want that we track your visit to our site you can disable tracking in your browser here:

Other external services

We also use different external services like Google Webfonts, Google Maps, and external Video providers. Since these providers may collect personal data like your IP address we allow you to block them here. Please be aware that this might heavily reduce the functionality and appearance of our site. Changes will take effect once you reload the page.

Google Webfont Settings:

Google Map Settings:

Google reCaptcha Settings:

Vimeo and Youtube video embeds:

Other cookies

The following cookies are also needed - You can choose if you want to allow them:

Privacy Policy

You can read about our cookies and privacy settings in detail on our Privacy Policy Page.

Privacy Policy
Accept settingsHide notification only