Django comes with an awesome feature
called database migrations. Database
migrations can be used to automatically
create and make changes to our database
based on what we change in the models. It
works by taking a look at the current
state of the database and the current state
of our models and seeing what the
differences are. So Django does this all
for us in the background and it saves us
a lot of time because it means that
Django manages all the changes to our
database on our behalf.
So we don't need to make any tedious
sql queries or commands or anything
to make the changes that we need when we
update our database with new models or
new fields. So let's create our first
django database migration. You create the
django database migrations using the
management command that comes with
django so let's minimize atom and load
up the git bash or the terminal window
and run our vagrant servers so type
vagrant up and if vagrant is already
running it should be quite quick and
then type vagrant ssh to connect to it.
Okay so now that we're logged on to the server
let's switch to our profiles api virtual
environment. So type work on profiles_api
and now we're going to change to the
project location on the server so CD /
vagrant / src / profiles_project
and from the profiles
project location we're going to run the
manage.py command so type python
manage.py and the command we're going to
use is the make migrations command. What
this is going to do is it's going to
check our models, it's going to check the
database and it's going to make a
migration file
that contains all of the changes from
what's existing in the database to what
we've changed in the models. Because this
is the first time we're running the
migrations currently there is no
database so Django will automatically
just create the migration that will set
up our database for the first time. So
type python manage make migrations and
hit enter. So what's happened here? Well
it appears that we have an error in our
models file and Django is kindly telling
us about this error now. So it's saying
in this file, in the models file that we
were working with earlier, on line 39
there's a problem with the doc string. So
let's go and fix that before we continue.
Load up atom editor and go back to our
models.py file and let's locate
line 39 in the system. So that's this
line here so the error is that we've
accidentally added an extra quotation
mark here, so delete the quotation mark
because there needs to be only three at
the beginning and three at the end. And
then save the file and let's head back
to the git bash. You can use
the up key to load the last command that we
called so you don't have to type it
again python manage.py make
migrations and hit enter.
Let's see if it works this time. There
you go so this time it worked.
It says here create model
user profile. So let's just load up our
atom editor and look at what's changed
here. You can see that within our
profiles API it's made a change to this
migrations directory and it's added a
new file called 0001 initial.
So we don't really need to worry
too much about what this does here but
let's look at it anyway just so that we
can get an understanding. So this is
created by Django automatically and what
this migration file does is it tells
Django how to set up our database
and because this is our first migration
it created the initial file so this
is the initial database setup when you
first run the app. So we don't need to
change this let's close this off and now
let's run our migration to create our
database. So head back to the git bash
and we're going to use the same manage.py
command but this time we're going
to call a migrate command. So python manage.py migrate
What this is going to do is
it's going to go through all of our database
migrations and it's going to run them on
our database. So hit enter you can see
that it's gone ahead and it's created a
bunch of things here and they've all
said ok which is good so it's run our
migration, where is our one? Our
one is here and these ones around it are
migration files that are included with
the different Django apps that we're
using. So because we're using the Django
auth app it's created the auth table.
Because we're using the Django sessions
it's created the sessions table and it's
created a table for the admin. So it's
created migrations or it's run the
migrations for all the different apps
that we're using in our project. And it's
created a sqlite file here so this
db.sqlite. Now I don't recommend
clicking on this because it's a big
binary file and if you click on it can
sometimes crash the atom editor. Okay so
let's just run the migrations again so
we can see what happens if we run it a
second time. You can see that because it
already ran these it already applied them to
the database the second time we ran it it
says there's no migrations to apply. So
Django can see that all of the
migrations we have in our system have
already been applied to our database and
that doesn't need to apply anymore. If we
were to create more changes or more
migrations then it would run the
differences from the last time we ran
the python manage migrate command. Okay
so that's how we
run our migrations and create our
migrations in the system. Now that we've
done that let's commit our changes to
git. So I'm going to load up a new git bash
window here and type cd workspace
profiles rest api and we're going to add
all of the files by typing git add dot
hit enter and we're going to commit our
changes so we're going to type git
commit -a for all, and m for message and
we're going to describe the changes that
we made which is "Added a custom user
profile model, a manager and the
migrations for our database." Close that off and
hit enter. Okay so our changes have been
committed to the database. That's how you
create a custom user model, custom user
manager and migrations in the
Django system and that's how you apply
the migration to the database. I hope you
found this video useful. Thank you so
much for watching and I'll see you in
the next lesson.