Junior — Middle
How to implement changing the database or updating the schema in Django using migrations?
sobes.tech AI
Answer from AI
In Django, a migration system is used to manage changes to the database, allowing you to create, modify, and delete tables and fields without losing data.
Main steps:
- Make changes to models (models.py), such as adding a new field or changing the type.
- Run the command
python manage.py makemigrations— it will create a migration file describing the changes. - Apply the migrations to the database with the command
python manage.py migrate.
If you need to change the database, simply modify the settings in settings.py (DATABASES), and then run migrations for the new database.
Example:
python manage.py makemigrations
python manage.py migrate
Thus, migrations allow you to manage the database schema in a sequential and safe manner.