Django migrations का उपयोग: model बदलाव सुरक्षित रूप से लागू करना
Django migration, model बदलाव को क्रमबद्ध Python फ़ाइल में दर्ज करता है और उसके operations को database schema पर लागू करता है। Model बदलने के बाद `makemigrations` से प्रस्ताव बनाएँ, उसकी समीक्षा करें और फिर `migrate` से लागू करें। Migration फ़ाइलों को code के साथ version control में रखें। क्रम केवल फ़ाइल के नंबर से तय न करें; dependencies और बने हुए SQL को देखें।
Django migrations का उपयोग: model बदलाव सुरक्षित रूप से लागू करना
Django migration, model बदलाव को क्रमबद्ध Python फ़ाइल में दर्ज करता है और उसके operations को database schema पर लागू करता है। Model बदलने के बाद makemigrations से प्रस्ताव बनाएँ, उसकी समीक्षा करें और फिर migrate से लागू करें। Migration फ़ाइलों को code के साथ version control में रखें। क्रम केवल फ़ाइल के नंबर से तय न करें; dependencies और बने हुए SQL को देखें।
यह मार्गदर्शिका Django 6.0 के आधिकारिक दस्तावेज़ों में दिए relational database workflow पर है (जाँच तिथि: 2026-11-23)। व्यवहार Django संस्करण और database backend के अनुसार बदल सकता है। उदाहरण केवल समझाने के लिए हैं, चलाए नहीं गए।
Migration फ़ाइल क्या दर्ज करती है?
makemigrations मौजूदा models की तुलना पुराने migrations से बनी model state से करता है। यह AddField, CreateModel और AlterField जैसे operations तथा अन्य migrations पर dependencies लिखता है। Django इन्हीं dependencies से execution graph बनाता है; वह केवल फ़ाइल नाम के नंबरों पर निर्भर नहीं करता। अलग apps के बीच आवश्यकता भी dependency से बताई जाती है, जैसे foreign key जिस table को संदर्भित करती है, उसका पहले मौजूद होना। Django 6.0 migrations
accounts.0005 ──────┐
├──> orders.0003
orders.0002 ────────┘यदि orders.0003, accounts.0005 और orders.0002 दोनों पर निर्भर है, तो उसे उन दोनों के बाद चलना चाहिए। समान नंबर वाली parallel शाखाएँ इस तरह रह सकती हैं, हालांकि उनके operations फिर भी टकरा सकते हैं।
Model बदलाव बनाना और लागू करना
मान लें कि Customer में optional display name जोड़ना है:
class Customer(models.Model):
email = models.EmailField(unique=True)
display_name = models.CharField(max_length=120, null=True, blank=True)Project का virtual environment सक्रिय करें और जाँचें कि settings सही database की ओर हैं। फिर app के लिए नाम वाला migration बनाएँ:
python manage.py makemigrations customers --name add_customer_display_nameOutput पढ़ें और customers/migrations/ के नए फ़ाइल को खोलें। Auto-detection models और migration files की state की तुलना करता है; यह आपके डेटा या उद्देश्य को पूरी तरह नहीं जानता। Rename को नया field जोड़कर पुराना हटाने के रूप में बनाया जा सकता है, जिससे पुराने मान खो सकते हैं। जाँचें कि operation डेटा सुरक्षित रखता है या नहीं और ज़रूरत पर migration संपादित करें। Django 6.0 migrations Command reference
लागू करने से पहले plan, backend-specific SQL और दर्ज status देखें:
python manage.py showmigrations customers --plan
python manage.py sqlmigrate customers 0002
python manage.py showmigrations customersshowmigrations --plan pending operations और dependency order दिखाता है; सामान्य सूची लागू migrations को [X] से चिह्नित करती है। sqlmigrate SQL दिखाता है और constraint नाम सुलझाने के लिए database connection उपयोग करता है। इसे target database से मेल खाते copy पर देखें। 0002 उदाहरण है; वास्तविक migration नाम उपयोग करें। Command reference
Development में समीक्षा के बाद migrations लागू करें:
python manage.py migrate
python manage.py showmigrations customersबिना arguments के migrate सभी pending migrations को dependencies के क्रम में लागू करता है। एक app देने पर भी दूसरी apps की migrations चल सकती हैं। बाद में [X] status देखें और model बदलाव तथा migration को साथ में version control में commit करें ताकि टीम और deployment environments को समान इतिहास मिले। Production में reviewed migration को deployment process से चलाएँ और परिणाम की पुष्टि करें। Django 6.0 migrations Command reference
Data migration और schema migration
Schema migration tables, columns, indexes या constraints बदलता है। Data migration मौजूदा rows को बदलता है। बड़े data transformation को structural बदलाव के साथ करने पर locks और डेटा हानि का जोखिम समझना कठिन हो सकता है। भरी हुई table में required field जोड़ते समय पहले nullable field जोड़ना, values भरना, फिर constraint लागू करना अधिक सुरक्षित चरण हो सकता है। सही क्रम database, डेटा की मात्रा और write traffic के लिए जाँचना चाहिए। Django 6.0 में migrations लिखना
RunPython से डेटा बदलते समय models.py से current model import न करें; उस migration समय की historical model लें:
from django.db import migrations
def fill_display_name(apps, schema_editor):
Customer = apps.get_model("customers", "Customer")
for customer in Customer.objects.filter(display_name__isnull=True).iterator():
customer.display_name = customer.email.split("@", 1)[0]
customer.save(update_fields=["display_name"])
class Migration(migrations.Migration):
dependencies = [("customers", "0002_add_customer_display_name")]
operations = [migrations.RunPython(fill_display_name, migrations.RunPython.noop)]यह काल्पनिक नियम है; इसे चलाया नहीं गया और यह दावा नहीं करता कि email का prefix उपयुक्त नाम होगा। Historical model उस समय के fields और relations रखता है। Current model import करने से नए database पर पुरानी migration चलाते समय विफलता हो सकती है। Reverse function noop बदले गए डेटा को वापस नहीं लाता। Rollback आवश्यक हो तो सुरक्षित reverse operation बनाएँ या migration को irreversible बताकर दर्ज करें। Migrations लिखना Historical models
Branch conflicts और merge migration
दो branches एक ही starting point से अलग migrations बना सकती हैं और समान नंबर इस्तेमाल कर सकती हैं। Django नामों और dependencies को देखता है, इसलिए यह अपने आप में error नहीं है। लेकिन दोनों सिरों के बने रहने पर graph में कई heads होंगे। पहले operations और dependencies देखें कि दोनों बदलाव साथ लागू हो सकते हैं या नहीं।
जहाँ उपयुक्त हो, makemigrations --merge heads को जोड़ने वाली खाली migration बना सकता है:
python manage.py makemigrations --merge customersयह node histories को जोड़ता है; operations को merge या business conflict को हल नहीं करता। Development में compatibility और क्रम जाँचें। गलत dependency edit से inconsistent history बन सकती है, जैसे dependency लागू न हो लेकिन आगे की migration लागू हो; तब Django आगे बढ़ने से मना करेगा। Django 6.0 history consistency
मौजूदा database और rollback
यदि tables पहले से हैं, migrate --fake-initial शुरुआती migration को applied चिह्नित कर सकता है, लेकिन यह पूरी जाँच नहीं है। Django बताए गए tables या columns के मौजूद होने की जाँच करता है; पूरे schema, types या constraints के मेल की नहीं। अलग से पुष्टि करें कि database प्रारंभिक migration से मेल खाता है और हाथ से बदला नहीं गया। --fake SQL चलाए बिना दर्ज state बदलता है और migration history को असली schema से अलग कर सकता है। मौजूदा tables migrate विकल्प
Rollback हमेशा सीधा undo नहीं होता। migrate app_label migration_name app को लक्ष्य बिंदु पर ले जाता है; पीछे जाने पर अन्य apps की dependent migrations भी हट सकती हैं। पहले --plan देखें और destructive या irreversible operations खोजें। कुछ IrreversibleError देते हैं। Backup और recovery plan रखें; यह न मानें कि एक command सभी डेटा लौटाएगा। Migrations reverse करना migrate command
Database backend के अंतर
PostgreSQL और SQLite, DDL transactions का समर्थन होने पर, migration operations को सामान्यतः एक transaction में चलाते हैं। MySQL और Oracle schema बदलावों के लिए यही guarantee नहीं देते, इसलिए विफलता के बाद manual cleanup चाहिए हो सकता है। MySQL operations locks या interruptions ला सकते हैं। SQLite कुछ बदलावों को नई table बनाकर, rows copy करके और पुरानी table बदलकर emulate करता है; बड़ी table पर यह धीमा हो सकता है। Project में उपयोग हो रहे version और engine की सीमाएँ देखें। Django 6.0 backend support
लागू करने से पहले
Model और migration को एक ही code change में तैयार करें। Operations और graph पढ़ें, showmigrations --plan से plan देखें और compatible database copy पर sqlmigrate का SQL जाँचें। Representative डेटा से development या staging में सत्यापन करें। Production से पहले backups, locks और runtime, स्वीकार्य write interruption तथा failure recovery की योजना बनाएँ। लागू होने के बाद status जाँचें। साझा या लागू migration को दोबारा लिखने के बजाय आगे के बदलाव के लिए नई migration जोड़ें।