Sobes.tech
Junior — Middle

How do you implement and manage database schema migrations in Django?

sobes.tech AI

Answer from AI

In Django, database schema migrations are implemented using the built-in migration system, which allows managing model changes and synchronizing them with the database.

Main steps:

  1. Creating migrations: after modifying models, run the command:
env/bin/python manage.py makemigrations

This creates migration files describing the changes.

  1. Applying migrations: to apply changes to the database, use:
env/bin/python manage.py migrate
  1. Managing migrations: you can rollback migrations, check status, and apply migrations for specific apps.

Django stores information about applied migrations in a special table django_migrations, which helps track the schema state.

Thus, migrations provide version control for the database schema and simplify the process of updating the structure during project development.