It’s very important to understand the folders and files that make up a Django project. In this lesson, you’re going to take a look at migrations and SQL databases.
projects folder, a folder called
migrations has been generated. If you run the development server, you’ll see that there are migrations that haven’t been applied.
The database isn’t currently set up, and that’s what migrations do. They take the code you have and apply it to create or change the database that you’re working with.
00:00 Welcome back! I don’t want to dwell on this for too long, but I think it’s really important to understand what are the folders and files that make up a Django project, so we will take a look at migrations and also take a peek into the SQL database in this video. Let’s start with migrations.
we’re starting up our project, right? And now I already opened up the terminal far enough. You’ve probably seen this before if you ran the server. It tells me this very warning-like looking message in red:
17 unapplied migration(s). So, here we’ve got the word popping up, migrations, and then it tells us the project may not work properly until the migrations are applied. And we see these things here:
Migrations take the code that we have in here and apply it to create or update or change the database that we’re working with. Now, again, Django is very helpful and tells us to just run
python manage.py migrate to apply them. So remember, always read what Django tells you. It’s very helpful.
And another thing here, before we actually go ahead and try that out, I want to show you a thing in
settings.py: there’s a
DATABASES section. The default one that comes with a Django app when you start it up is just a SQLite database. We’ve got an
'ENGINE' that tells us, “Use this connector to allow us to interact with SQLite.” We’ve got a
'NAME', and the default
'NAME' is just
'db.sqlite3', which you might have seen around here. There it is, okay.
02:50 So, this is our database in the Django project. By default Django works with a file-based SQL database—that’s SQLite—so we don’t need to run a separate database server, but everything gets stored inside of here. And that’s just a file, so—you might already know that there’s some limitations to this, for reading and writing to the file.
You wouldn’t want to use this in production because it would be very slow, but for development, it’s perfectly fine and it takes away complications that we have with otherwise setting up databases. Eventually, you’re going to look into this more, and it’s pretty simple to change out just these settings here in the
settings file to connect to a different type of database—for example, a Postgres server that you have running somewhere.
Okay. But for now, we are going to write to this database file
db.sqlite3, and we’ve learned that our migrations are the changes that happen to the database. So inside of this folder, once we go ahead and follow Python’s tips here—I’m going to open up a new terminal tab for that.
what is it suggesting?
migrate. So, it’s just telling us to migrate. There’s actually two steps for this. The first step is to say
makemigrations, and we can specify which app we want to
Then, you see it tells us that it created the migrations for
Create model Project. So the default ones by Django already have
makemigrations applied, that’s why you can run
migrate right away. But for the new one,
04:47 You generally are not going to have to deal with this at all, but I want to just take a look so that you know what’s going on, on a very rough level. If you open up this file, there’s lots of Python code in here and we don’t need to understand this, but just know that what it does—this is the step in between translating to SQL.
So if you run
makemigrations, this migration file gets generated, and it’s, essentially, a description of what’s going to happen when this migration gets applied. Nothing has happened yet to the actual database, but this file got created. So now, we’re on the second command, the one that’s been suggested to us here.
If I say this, it’s going to take the migrations file and apply the migrations to the database. So running this: blah, blah, blah, blah, we got lots
OKs. And you see a lot of those
OKs, they don’t actually apply to our
projects app that we created, but to those other apps that Django already comes with:
contenttypes, you can see them here. Those migrations already existed before, so now they’re just getting applied—blah, blah, blah, blah—all the way down.
Now, our database is set up. Before, we didn’t have anything in there yet. So, it’s important to run these two commands:
makemigrations if you create a new app, and then
migrate to actually apply the migrations.
06:45 Think of it as what we talked about before, a relational database, and now we’re just going to take a peek inside of that. Also, just so that you have an idea of what’s going on in there without digging too deeply. I’m back here with the finished app, just so that we can see, also, something with data.
07:03 One thing that the professional PyCharm version has included is a database viewer, so I’m going to open up that one. If you don’t have this, if you just have the community edition, there’s free tools out there—“SQLite database inspector,” just Google it—and you can find something like that.
With the database viewer, I can take a look inside, so just opening up some of these things. And here you see all the tables that we have. Those are the ones that got created when we ran
migrate, just before.
We can see the
title—so, the columns in the same structure that we’re used to seeing like a table. Because that’s all it is, like, these ones are all tables that got created. This one has the data inside of it.
08:56 Okay. “Show me a diagram of the whole database.” So, this now includes everything, all the authentication tables that are set up by default in Django, but also the things that we’re going to create. So here’s the blog tables, here’s our project table, the one that we just created. That just gives you a nice way to inspect what’s going on in your database, if you’re interested in that side.
09:21 If you’re not, you’re lucky because you don’t really have to deal with it when you’re using Django. So, this is my little excursion inside of a SQLite file, so that you know what’s actually happening in there, and that there’s just a table structure—as with any relational database—contained within one file. And that sums it up for the database part for now.
Become a Member to join the conversation.