Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Migrations: SQLite

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.

Inside your 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.

00:18 You might’ve seen this folder generated here. It’s called migrations, inside of your project. Maybe you don’t have it yet, but usually, it comes right away. You can see it’s empty right now.

00:29 So, what’s this folder about? For this, I want to show you something that we’ve blissfully ignored, so far. If I go ahead here and I run the development server…

00:45 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: admin, auth, contenttypes, sessions.

01:12 All right. So, first of all, again, this is your friend, right? We’ve got nice error messages here in Django. It’s just giving us some information. You might have seen those before.

01:25 Let’s remember quickly where we’ve seen them: it was in our management app in settings.py.

01:33 So, if we scroll all the way to the top where we added our first app projects, way back in section two, I think? We read over those ones here, you see? admin, auth, contenttypes, sessions.

01:46 So, these first four of those, they apply to other installed apps. What this is telling us, essentially, it’s just that the database is currently not yet set up, and that’s what migrations do.

01:57 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.

02:18 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.

03:10 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.

03:34 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.

03:54 I’ll say python manage.py

03:59 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 makemigrations for.

04:13 For now, I’m just going to leave it empty, it should apply to all of them.

04:18 Then, you see it tells us that it created the migrations for projects/...initial.py: 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:35 our Project model here, this just got created. So inside of our projects app, we can now see this 0001_initial.py migration file sitting.

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.

05:07 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.

05:29 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: admin, auth, 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.

05:58 And then we have projects here—ours. That’s exactly this file here that we created with running makemigrations. This one also gets applied in this one migration.

06:09 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:21 And now, if we go ahead and run our server—I’m going to stop it here.

06:27 If I run the server again,

06:31 boom. Those scary, but actually helpful, messages are gone because all the migrations are applied. Sweet! Now, let’s take a look at what happens inside of a SQLite file.

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.

07:21 It makes it nice because it gives you a visual way to look into your database. You see, we have it here: db.sqlite3 (database sqlite3).

07:29 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.

07:43 Let’s take a look at one that has some interesting data in there. So, projects_projectour Project model that we just created before, I can now inspect it.

07:54 I’m going to look at this.

07:59 It queries our database, and now we can see in here. That’s the contents of the database.

08:07 We can see the id, 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:19 Sweet. So, there’s some other cool things you can do with the database inspector. For example, we could say…

08:32 “Show me a diagram.”

08:39 This is pretty small…

08:44 not great.

08:46 Okay.

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.

09:42 Let’s move on into more familiar territory.

Avatar image for Martin Breuss

Martin Breuss RP Team on May 14, 2020

Hi @bluebe55yPie and welcome to the (horrible) world of cross-browser inconsistencies… 😱

One thing upfront: from all I know this is not a Django issue to fix, but something that you will have to deal with in your HTML+CSS. Cross-browser styling lives in the realms of Front-End Web Development 😌

Here are two links that I found that you could look into if you want to go down that path. Just keep in mind that it might be a tricky task to get it all done. Internet Explorer is a known … challenge in the front-end web development world!

Hope this helps and gets you on the way!

Avatar image for bluebe55yPie

bluebe55yPie on May 14, 2020

Hi Martin, Thank you so much for your quick reply :) Good to know that it’s a Front End Web Development, so it narrows it down a bit to just HTML and CSS I need to deal with! I think I may have got myself confused as I thought it was something I need to change in Django and I made it all complicated for myself :( Hahaha, yes, I’ve noticed that Internet Explorer can be a nightmare from all the things I’ve read online, so I’m bracing myself for the challenge! Thank so much for the links and the help! It’s massively appreciated! I can now move forward in the right direction. :) :)

Avatar image for santhoshmallojwala

santhoshmallojwala on Sept. 10, 2020

Hi Martin,

I am just wondering how the sample data got inserted into projects_project table. Because we have not inserted any rows or test data into it rather we just created the table using the models object.

For example, I am using MySQL and able to connect to it from Django app. Also, I inserted few records in the table manually. How can I read those records in application.

Avatar image for Martin Breuss

Martin Breuss RP Team on Sept. 10, 2020

Hi @santhoshmallojwala. Yes, you won’t see any data in your tables yet, because we haven’t put anything in there yet. If you double-check around this time of the video, I mention that here we’re looking at the finished project, so that you can actually see some data in the database file :)

You’ll be adding data to your database in just a bit, in the Django Shell video that’s coming up.

Avatar image for santhoshmallojwala

santhoshmallojwala on Sept. 14, 2020

Thank you Martin :)

Avatar image for iwatts

iwatts on Sept. 22, 2020

Hi - Just in case anyone else is having the same problem:

  • Problem: I have run the migrations which all seemed to work succesfully but when I open up the database in the Database Viewer (Professional Edition) there is no schema or anything - just completely empty
  • Solution: When I clicked on Data Source Properties it told me I had missing drivers - once I installed them and refreshed everything appeared

Hope that helps someone.

Avatar image for Vasanth Baskaran

Vasanth Baskaran on Aug. 20, 2021

Yipee, Going through all the comments itself is a great learning experience…(giving myself a pat on my back)

Thanks @Martin for the wonderful byte sized yet powerful course videos!

Avatar image for hillchristop

hillchristop on Dec. 1, 2023

When I typed python manage.py makemigrations, I got a response that says “No changes detected.

Note: the following is what I have in the settings.py file:

"""
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # my apps
    'projects',
]
"""

How can I fix this problem? Thanks!

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Dec. 5, 2023

@hillchristop There could be a couple of reasons. Maybe you’ve already run the migrations? Did you spell the project name correctly? Are the models defined in the right place? What’s the current state of your database? These are just a few questions, but there could be more.

The quickest fix would be to delete the migration scripts generated by Django and try again. If that doesn’t help, then something must be wrong with the code. You can always download the supporting materials to compare it with your project.

Avatar image for Martin Breuss

Martin Breuss RP Team on Dec. 6, 2023

@hillchristop also from the code you shared, it seems like you commented-out the INSTALLED_APPS? I assume that this is just a formatting hiccup when posting, but if it’s also wrapped in triple-double-quotes in your code, then that would cause the migrations not to work.

Become a Member to join the conversation.