← Back to Django Course | Chapter 5: Migrations & Database Management | Lesson 3 of 8

migrate नामक Command

migrate सभी written-down migration instructions लेता है और असल database tables बनाता या update करता है।
Syntax
markup
python manage.py migrate
python manage.py migrate app_name

सभी Pending Migrations Apply करना

बिना किसी arguments के migrate चलाना हर installed app में हर वह migration apply करता है जो अभी तक apply नहीं हुई, correct dependency order में।

उदाहरण: Applying All Pending Migrations

Django prints 'Applying blog.0002_add_published_flag... OK' for each migration it runs against the database.

bash
python manage.py migrate

⚠️ Run this in your own terminal or Node.js environment.

एक Single App को Migrate करना

migrate के बाद एक app का नाम pass करना सिर्फ उस app की pending migrations apply करता है, बाकी project को untouched छोड़ते हुए।

उदाहरण: Migrating a Single App

Only migrations belonging to the blog app are applied; other apps' pending migrations stay unapplied.

bash
python manage.py migrate blog

⚠️ Run this in your own terminal or Node.js environment.

Django Applied Migrations कैसे Track करता है

Django एक django_migrations नाम की table रखता है जो उस app और migration name को record करती है जिसे यह पहले ही apply कर चुका है, इसलिए migrate फिर से चलाना हमेशा safe है और पुराने changes को फिर apply नहीं करेगा।

Note: migrate को बार-बार चलाना harmless है — यह हमेशा पहले इस table को check करता है।

उदाहरण: How Django Tracks Applied Migrations

This queries Django's own migration-tracking table to list which blog migrations have already been recorded as applied.

bash
python manage.py shell
>>> from django.db.migrations.recorder import MigrationRecorder
>>> MigrationRecorder.Migration.objects.filter(app='blog')

⚠️ Run this in your own terminal or Node.js environment.

एक Migration को Roll Back करना

app के बाद एक specific migration name pass करना database को उस point पर roll back करता है, इसके बाद apply हुई हर migration को reverse करते हुए।

Warning: ऐसी migrations reverse करना जो पहले से data drop कर चुकी हैं (जैसे एक column हटाना) permanently वह data खो सकता है।

उदाहरण: Rolling Back a Migration

This reverts the blog app back to the state it was in right after migration 0001, undoing 0002 and anything after it.

bash
python manage.py migrate blog 0001

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. makemigrations पहले कभी चलाए बिना migrate चलाना, फिर यह सोचना कि database recent model change को reflect क्यों नहीं कर रहा।
  2. एक teammate की नई migration files को version control से pull करने के बाद migrate चलाना भूल जाना।
  3. यह मान लेना कि migrate हर बार हर migration फिर से चलाता है, जब यह सिर्फ उन्हें apply करता है जो अभी तक apply नहीं हुई।
चैप्टर सारांश
  • migrate pending migrations को असली database पर apply करता है।
  • Django track करता है कि कौन सी migrations पहले ही एक special django_migrations table में चल चुकी हैं।
  • कुछ भी change न होने के बाद migrate फिर से चलाना कुछ नहीं करता — इसे अक्सर चलाना safe है।
  • migrate पहली बार चलने पर Django की अपनी built-in tables जैसे auth और sessions भी set up करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.