Okay.
Let's talk about the migrations in this video.
So what migrations are if we come back to the run server, you can see here, if I'll make a more space
here, you can see whenever I run the server and that was the command I run with the manage by clicking
on the button here.
And then we have some information here you have 17 unapplied migrations.
So what does it mean?
Unapplied migration?
It means that we have some data in our project and application that hasn't been applied to our database
yet.
So in order to make the to set up our database with the new data we have, it's not new data, it's
new schema we have and would like to keep it in our application.
We need to do a migrate.
So here we have the comment we need to run our migration, migrate means.
So I'll copy this and I will go to the terminal.
Inside here I'm inside virtual Environment inside tutorial.
So what I can do is I can paste this.
So basically we have I need to use Python3 Manage.py the same way as we use it and then migrate.
That means we'll apply the current migrations that will be applied from the project, the default that
Django created for us, and it will create that schema on the database.
At this point we have database SQLite here, so if I will click Enter, you can see here that that many
changes has been applied for our migration, for our database and from where this is coming from if
we go to our.
Project, which is first, and then we open the settings.
We'll scroll here and we can see see installed apps In that section, we tell Django what kind of application
we'd like to use within our project.
And we have, for example, here admin.
And if we go here, you can see that some of these migrations let me.
Find it here.
So we have admin, this one.
So we have migration from this application applied to our database.
You have also out here and then you have out here.
So each of this application has a migration files that we can use to apply to our database.
And you can see we have a few of them and basically they are all coming from here.
So that's like an application has been added automatically to our project and we can use it straight
away.
So the admin section I've showed you before, it's coming from here, but in order to use it you need
to do a migration.
So we applied all migrations from this installed apps.
Okay, but inside our application demo we also have folder migrations.
If I will open here.
We don't have any files here except of init.
That means this init is just a initiate some command, so it's not something that we might be interested
in and we can consider this folder as empty.
Okay.
So what we can do if I go to the models here and that's demo models in this, as I said before, in
this.
File will create our models.
If we create our model here.
Here it will be basically kind of creating some kind of container or a class that we can store some
information in our database.
And I will show you how to create a class based models.
So basically what we need to do is we need to create a class.
And then we name that class.
Let's say it's gonna be a book, and that is Capital B, But by convention, we name our classes with
capital letters so we can do book here, and then we can use some built in Django functionality.
So inside here I will do models and then I will do model models.
Is that what we import at the top of this file?
So from the models I import model, I will add it to my book.
So inside book I can use it.
So if we have proper indentation here, we can add some fields into the book.
Let's say we would like to have title and then I will explain more about the different types and that
we have available here.
But at the moment I would like to show you how the migrations work.
So model models.
Charfield.
Max length.
For example, we can have it.
36.
So what I did here is I've created a new object and that object will be stored in our database.
And then this object will have a title for the moment.
And we for this title, we'd like to keep it as a char field, which is a simple text, a string, and
we can say Max, length of this will be 36.
Okay, so what's happening now?
If I will go right now to the to our terminal and I will try to run this manage.py migrate again.
It's not going to do anything because we already applied our migrations to our database.
But this is new.
So what we will need to do actually is we need to take whatever changes we have in this models file.
We need to create migrations and then that migrations will create a new file in this.
And then using command migrate, we can actually apply this migration files into our database.
Okay, let's do that.
And I will also show you how this is.
It's not going to work and I will explain you what we need to do to make it work.
So Python three manage pi the same as we used before and then space.
And then we will use a command, create migrations capital s at demo at the end.
So manage.py create migrations.
That command here will take all the changes here.
So whatever you will add new or change anything here and it will create a file for you.
And we should expect migrations file created in this migration folder.
So try to run it now.
And you can see here and I made a mistake because it's not create migrations, it's make migrations.
So I will do make migrations sorry for a mistake and then make migrations.
No changes detected.
So why this is happening?
We are we have our changes here, but not it has been detected.
The reason for this is we need to go to the settings and inside installed apps.
We need to add our app manually here.
So our application is demo.
So basically what we need to do is at the end of this list, inside the single comma, we can type in
demo that's the name of our application.
And just to be safe, we can put extra comma on the end of this line.
Just if we add another one, there will be no error.
So basically what's happening now is we are telling Django Project that we have our application and
whatever we will have a new application.
We need to add it inside installed apps.
So I'll will save it, save it here and let's make the same command again.
So Python3 Manage.py make migrations and also take a look at the migration folder here.
So if I run it now, you can see here that demo migrations, this file has been created and at the moment
we don't see it here.
But if I will click here, it will appear in a second and you can see here the file is there.
So if I open this file, it's basically a set of rules how to apply our class into our database.
But at this point, this has not been added yet to our database.
The make migrations will only create that file and the 001 initial is the file created with this command.
Make migrations to apply this migrations on the database.
We need to do it.
Migrate again.
Migrate.
And you can see here that demo 001 initial, which is this file has been applied to our database.
And at this point, our changes models, which we have here, are inside our database.
It doesn't mean we have some records in database, but this database is prepared now to have a to store
our books with titles that will be a string up to 36 characters long.
So this is the way you do migrations.
Whatever you will try to store in your database, you will need to go to the models and add it.
So basically you don't need to keep just one field at a time.
You might have more than one.
And if you want to imagine, this will be like a table.
So you have one table for books and then each column will be different field we have.
So for one column you might have a title and then each row will be a record set of record.
Store it in the database.
That's how it works.
And if you would like to have more, then you will add another field here.
And then what you need to do is make migrations.
It will create another file in a migration folder and we'll do that later in the tutorial.
And also once you have that new file, you need to run manage.py migrate and that changes will be applied
to the database in the moment when they will be applied in a database.
You can add and manage the records in the database with that fields.