migrate नामक Command
In this page:
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.
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.
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 नहीं करेगा।
उदाहरण: How Django Tracks Applied Migrations
This queries Django's own migration-tracking table to list which blog migrations have already been recorded as applied.
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 करते हुए।
उदाहरण: 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.
python manage.py migrate blog 0001
⚠️ Run this in your own terminal or Node.js environment.
- makemigrations पहले कभी चलाए बिना migrate चलाना, फिर यह सोचना कि database recent model change को reflect क्यों नहीं कर रहा।
- एक teammate की नई migration files को version control से pull करने के बाद migrate चलाना भूल जाना।
- यह मान लेना कि 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 करता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: