Applying Django Migrations
Migrations have to be applied to make changes to the database. In this lesson you’ll see how to use the management command migrate
to apply migrations.
00:01
Let’s look at applying migrations. You’ve seen how to create migrations, but at the moment they haven’t been applied. Applying them involves a separate command migrate
.
00:20 As you can see, aside from the migration you’ve seen created, there are a number of other migrations. These are all standard and are used when Django is first installed to create the database tables that it needs to run.
00:34
Let’s have another look at the database using dbshell
.
00:44
As you can see, there are now a number of tables present, including the one for our app, historical_data_pricehistory
. Let’s take a closer look at it using the .schema
command.
01:02
As you can see here, the fields from models.py
—"date"
, "price"
, and "volume"
—are all present in the database, along with "id"
, a primary key which is used to ensure all records in the database are unique.
01:22 What happens if you try and apply the migrations again? As you can see, Django has kept track of which migrations have been applied and won’t apply them again.
Dee82 on Nov. 7, 2023
Before starting off creating and applying migrations, it would be helpful to have some guidance around which tools are being used and need to be installed to work side by side with this tutorial. The python manage.py dbshell command doesn’t appear to work for my setup for example.
Become a Member to join the conversation.
koutsellisthemistoklis on March 4, 2020
Can you please explain a little more each of the commands you are using and make and eliminate the speed of given information? Thank you!