Conteúdo do Curso
Django: Build Your First Website
Django: Build Your First Website
Migrations
The Python code operates independently of the database. To reflect the changes made in our Python code in the database structure, we need to generate and apply migrations.
Migration is the automated way of making changes to your database structure, ensuring that your database stays up to changes you make to your code.
When you create a model, you need to run the following commands in the Terminal:
If the commands above doesn't work, try python3 ...
.
Description of makemigrations
The python manage.py makemigrations
command records the changes in the database and creates migrations.
Migrations are represented as Python files (.py
format).
The files in this folder contain scripts and data for migration to change the structure of the database.
Note
This is the process of the framework. Migrations work automatically. You do not need to touch these files. Only sometimes, there are moments when it is desirable to delete the migration and create a new one, but this is only a last resort.
Description of migrate
To apply created migrations, you need to run another command in the Terminal.
The command python manage.py migrate
executes migrations scripts (.py
files) and sends queries to the database.
After executing migrations, the terminal output will be as follows, indicating that everything was done correctly.
When you move the Django project to another device, you can run the migrate
command only because the migrations are already saved in files.
Obrigado pelo seu feedback!