Bird
0
0

In a Django production environment, how should you safely add a new non-nullable field to a large existing table to avoid downtime?

hard📝 Conceptual Q8 of 15
Django - Deployment and Production
In a Django production environment, how should you safely add a new non-nullable field to a large existing table to avoid downtime?
ADrop the table and recreate it with the new field included
BDirectly add the non-nullable field with a default value in a single migration
CAdd the field as nullable first, backfill data in batches, then alter the field to non-nullable
DAdd the field as non-nullable and rely on Django to handle default values automatically
Step-by-Step Solution
Solution:
  1. Step 1: Add the field as nullable

    This avoids locking the table and allows the migration to run quickly without downtime.
  2. Step 2: Backfill existing rows in batches

    Populate the new field with appropriate values gradually to prevent long locks.
  3. Step 3: Alter the field to non-nullable

    Once all rows have valid data, change the field to non-nullable safely.
  4. Final Answer:

    Add the field as nullable first, backfill data in batches, then alter the field to non-nullable -> Option C
  5. Quick Check:

    Stepwise migration avoids downtime and locks [OK]
Quick Trick: Add nullable field first, backfill, then make non-nullable [OK]
Common Mistakes:
MISTAKES
  • Adding non-nullable field directly causing table locks
  • Assuming Django handles default values automatically
  • Dropping and recreating tables in production

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes