WEBVTT

00:01.620 --> 00:02.220
Okay.

00:02.220 --> 00:06.540
Let's talk about the migrations in this video.

00:06.570 --> 00:13.950
So what migrations are if we come back to the run server, you can see here, if I'll make a more space

00:13.950 --> 00:21.600
here, you can see whenever I run the server and that was the command I run with the manage by clicking

00:21.600 --> 00:22.740
on the button here.

00:22.740 --> 00:28.620
And then we have some information here you have 17 unapplied migrations.

00:28.620 --> 00:30.300
So what does it mean?

00:30.330 --> 00:32.100
Unapplied migration?

00:32.310 --> 00:40.200
It means that we have some data in our project and application that hasn't been applied to our database

00:40.200 --> 00:40.770
yet.

00:40.770 --> 00:49.020
So in order to make the to set up our database with the new data we have, it's not new data, it's

00:49.020 --> 00:53.310
new schema we have and would like to keep it in our application.

00:53.310 --> 00:55.560
We need to do a migrate.

00:55.560 --> 01:01.850
So here we have the comment we need to run our migration, migrate means.

01:01.920 --> 01:05.340
So I'll copy this and I will go to the terminal.

01:05.340 --> 01:09.120
Inside here I'm inside virtual Environment inside tutorial.

01:09.120 --> 01:12.390
So what I can do is I can paste this.

01:12.390 --> 01:22.410
So basically we have I need to use Python3 Manage.py the same way as we use it and then migrate.

01:22.410 --> 01:29.220
That means we'll apply the current migrations that will be applied from the project, the default that

01:29.220 --> 01:34.680
Django created for us, and it will create that schema on the database.

01:34.680 --> 01:43.380
At this point we have database SQLite here, so if I will click Enter, you can see here that that many

01:43.380 --> 01:50.850
changes has been applied for our migration, for our database and from where this is coming from if

01:50.850 --> 01:52.140
we go to our.

01:54.940 --> 01:58.450
Project, which is first, and then we open the settings.

02:00.790 --> 02:09.370
We'll scroll here and we can see see installed apps In that section, we tell Django what kind of application

02:09.370 --> 02:11.890
we'd like to use within our project.

02:11.890 --> 02:14.350
And we have, for example, here admin.

02:14.350 --> 02:21.850
And if we go here, you can see that some of these migrations let me.

02:22.590 --> 02:23.700
Find it here.

02:23.700 --> 02:25.670
So we have admin, this one.

02:25.680 --> 02:31.650
So we have migration from this application applied to our database.

02:31.680 --> 02:36.390
You have also out here and then you have out here.

02:36.390 --> 02:44.550
So each of this application has a migration files that we can use to apply to our database.

02:44.550 --> 02:49.590
And you can see we have a few of them and basically they are all coming from here.

02:49.590 --> 02:55.860
So that's like an application has been added automatically to our project and we can use it straight

02:55.860 --> 02:56.340
away.

02:56.340 --> 03:02.790
So the admin section I've showed you before, it's coming from here, but in order to use it you need

03:02.790 --> 03:04.170
to do a migration.

03:04.170 --> 03:08.460
So we applied all migrations from this installed apps.

03:08.520 --> 03:14.490
Okay, but inside our application demo we also have folder migrations.

03:14.490 --> 03:15.780
If I will open here.

03:15.780 --> 03:19.530
We don't have any files here except of init.

03:19.560 --> 03:27.780
That means this init is just a initiate some command, so it's not something that we might be interested

03:27.780 --> 03:30.810
in and we can consider this folder as empty.

03:31.050 --> 03:31.410
Okay.

03:31.410 --> 03:39.480
So what we can do if I go to the models here and that's demo models in this, as I said before, in

03:39.480 --> 03:40.020
this.

03:40.800 --> 03:43.740
File will create our models.

03:44.160 --> 03:45.930
If we create our model here.

03:46.080 --> 03:53.280
Here it will be basically kind of creating some kind of container or a class that we can store some

03:53.280 --> 03:55.130
information in our database.

03:55.140 --> 03:59.270
And I will show you how to create a class based models.

03:59.280 --> 04:03.420
So basically what we need to do is we need to create a class.

04:05.310 --> 04:07.020
And then we name that class.

04:07.020 --> 04:15.720
Let's say it's gonna be a book, and that is Capital B, But by convention, we name our classes with

04:15.720 --> 04:24.800
capital letters so we can do book here, and then we can use some built in Django functionality.

04:24.810 --> 04:33.090
So inside here I will do models and then I will do model models.

04:33.090 --> 04:37.320
Is that what we import at the top of this file?

04:37.320 --> 04:41.130
So from the models I import model, I will add it to my book.

04:41.130 --> 04:44.190
So inside book I can use it.

04:44.190 --> 04:49.140
So if we have proper indentation here, we can add some fields into the book.

04:49.140 --> 04:57.480
Let's say we would like to have title and then I will explain more about the different types and that

04:57.480 --> 04:59.070
we have available here.

04:59.070 --> 05:02.400
But at the moment I would like to show you how the migrations work.

05:02.400 --> 05:05.280
So model models.

05:07.660 --> 05:08.590
Charfield.

05:12.110 --> 05:12.920
Max length.

05:12.950 --> 05:15.590
For example, we can have it.

05:15.890 --> 05:17.090
36.

05:17.270 --> 05:27.800
So what I did here is I've created a new object and that object will be stored in our database.

05:27.800 --> 05:31.310
And then this object will have a title for the moment.

05:31.310 --> 05:38.060
And we for this title, we'd like to keep it as a char field, which is a simple text, a string, and

05:38.060 --> 05:41.060
we can say Max, length of this will be 36.

05:41.300 --> 05:43.190
Okay, so what's happening now?

05:43.190 --> 05:53.630
If I will go right now to the to our terminal and I will try to run this manage.py migrate again.

05:54.850 --> 06:00.580
It's not going to do anything because we already applied our migrations to our database.

06:00.580 --> 06:01.960
But this is new.

06:01.960 --> 06:08.230
So what we will need to do actually is we need to take whatever changes we have in this models file.

06:08.230 --> 06:14.050
We need to create migrations and then that migrations will create a new file in this.

06:14.050 --> 06:20.380
And then using command migrate, we can actually apply this migration files into our database.

06:20.620 --> 06:21.760
Okay, let's do that.

06:21.760 --> 06:23.980
And I will also show you how this is.

06:24.220 --> 06:29.110
It's not going to work and I will explain you what we need to do to make it work.

06:29.110 --> 06:36.910
So Python three manage pi the same as we used before and then space.

06:36.910 --> 06:44.260
And then we will use a command, create migrations capital s at demo at the end.

06:44.260 --> 06:46.330
So manage.py create migrations.

06:46.330 --> 06:50.380
That command here will take all the changes here.

06:50.380 --> 06:56.450
So whatever you will add new or change anything here and it will create a file for you.

06:56.450 --> 07:01.070
And we should expect migrations file created in this migration folder.

07:01.070 --> 07:02.870
So try to run it now.

07:03.440 --> 07:10.520
And you can see here and I made a mistake because it's not create migrations, it's make migrations.

07:10.610 --> 07:17.600
So I will do make migrations sorry for a mistake and then make migrations.

07:17.900 --> 07:19.520
No changes detected.

07:19.520 --> 07:21.890
So why this is happening?

07:21.890 --> 07:26.050
We are we have our changes here, but not it has been detected.

07:26.060 --> 07:31.310
The reason for this is we need to go to the settings and inside installed apps.

07:31.310 --> 07:34.190
We need to add our app manually here.

07:34.190 --> 07:36.770
So our application is demo.

07:36.770 --> 07:43.940
So basically what we need to do is at the end of this list, inside the single comma, we can type in

07:43.940 --> 07:46.730
demo that's the name of our application.

07:46.730 --> 07:52.640
And just to be safe, we can put extra comma on the end of this line.

07:52.640 --> 07:55.670
Just if we add another one, there will be no error.

07:55.670 --> 08:02.600
So basically what's happening now is we are telling Django Project that we have our application and

08:02.600 --> 08:04.520
whatever we will have a new application.

08:04.520 --> 08:07.100
We need to add it inside installed apps.

08:07.100 --> 08:11.900
So I'll will save it, save it here and let's make the same command again.

08:11.900 --> 08:17.780
So Python3 Manage.py make migrations and also take a look at the migration folder here.

08:17.780 --> 08:26.780
So if I run it now, you can see here that demo migrations, this file has been created and at the moment

08:26.780 --> 08:27.830
we don't see it here.

08:27.830 --> 08:33.770
But if I will click here, it will appear in a second and you can see here the file is there.

08:33.770 --> 08:41.840
So if I open this file, it's basically a set of rules how to apply our class into our database.

08:41.840 --> 08:48.050
But at this point, this has not been added yet to our database.

08:48.080 --> 08:57.190
The make migrations will only create that file and the 001 initial is the file created with this command.

08:57.190 --> 09:01.060
Make migrations to apply this migrations on the database.

09:01.060 --> 09:02.050
We need to do it.

09:02.050 --> 09:03.220
Migrate again.

09:04.570 --> 09:05.470
Migrate.

09:06.580 --> 09:14.680
And you can see here that demo 001 initial, which is this file has been applied to our database.

09:14.680 --> 09:21.880
And at this point, our changes models, which we have here, are inside our database.

09:21.910 --> 09:28.810
It doesn't mean we have some records in database, but this database is prepared now to have a to store

09:28.810 --> 09:35.650
our books with titles that will be a string up to 36 characters long.

09:35.650 --> 09:38.560
So this is the way you do migrations.

09:38.560 --> 09:44.620
Whatever you will try to store in your database, you will need to go to the models and add it.

09:44.620 --> 09:49.690
So basically you don't need to keep just one field at a time.

09:49.690 --> 09:51.430
You might have more than one.

09:51.430 --> 09:56.720
And if you want to imagine, this will be like a table.

09:56.720 --> 10:03.260
So you have one table for books and then each column will be different field we have.

10:03.260 --> 10:09.860
So for one column you might have a title and then each row will be a record set of record.

10:09.860 --> 10:11.450
Store it in the database.

10:11.870 --> 10:13.070
That's how it works.

10:13.070 --> 10:17.330
And if you would like to have more, then you will add another field here.

10:17.330 --> 10:20.390
And then what you need to do is make migrations.

10:20.390 --> 10:26.360
It will create another file in a migration folder and we'll do that later in the tutorial.

10:26.360 --> 10:34.730
And also once you have that new file, you need to run manage.py migrate and that changes will be applied

10:34.730 --> 10:38.030
to the database in the moment when they will be applied in a database.

10:38.030 --> 10:43.760
You can add and manage the records in the database with that fields.
