WEBVTT 1 00:00:00.005 --> 00:00:01.009 - [Instructor] While the Django models 2 00:00:01.009 --> 00:00:04.006 define the expected structure of our database, 3 00:00:04.006 --> 00:00:05.008 migrations are responsible 4 00:00:05.008 --> 00:00:07.006 for creating the necessary scripts 5 00:00:07.006 --> 00:00:09.009 to change this structure through time 6 00:00:09.009 --> 00:00:12.008 as we update our code to change our models. 7 00:00:12.008 --> 00:00:15.008 There are several cases in which a migration is needed. 8 00:00:15.008 --> 00:00:17.004 When a new model is created, 9 00:00:17.004 --> 00:00:20.006 a migration creates the corresponding database table. 10 00:00:20.006 --> 00:00:21.009 Migrations are also needed 11 00:00:21.009 --> 00:00:25.002 when a field is added or removed from an existing model, 12 00:00:25.002 --> 00:00:28.005 or, when attributes of a field have changed. 13 00:00:28.005 --> 00:00:30.005 All of these changes to a model's file 14 00:00:30.005 --> 00:00:33.001 need a corresponding change to the database, 15 00:00:33.001 --> 00:00:36.003 and for these purposes migrations need to be created, 16 00:00:36.003 --> 00:00:37.006 and then run. 17 00:00:37.006 --> 00:00:40.003 The first migration created for a new Django app 18 00:00:40.003 --> 00:00:43.002 will create tables for the models that are defined. 19 00:00:43.002 --> 00:00:46.005 These migrations are called initial migrations. 20 00:00:46.005 --> 00:00:48.006 We've defined models for our app, 21 00:00:48.006 --> 00:00:51.002 but we have yet to create these initial migrations 22 00:00:51.002 --> 00:00:53.003 and we'll tackle this in a moment. 23 00:00:53.003 --> 00:00:55.006 The commands for working with migrations are 24 00:00:55.006 --> 00:00:58.007 makemigrations, showmigrations and migrate. 25 00:00:58.007 --> 00:01:03.000 The makemigrations command generates migration files. 26 00:01:03.000 --> 00:01:04.007 It reads the current model's file 27 00:01:04.007 --> 00:01:06.007 and inspects the current state of the database 28 00:01:06.007 --> 00:01:09.002 to determine what changes need to be made 29 00:01:09.002 --> 00:01:12.007 to make the database structure match the model's file. 30 00:01:12.007 --> 00:01:15.000 Those files are placed in the migration's folder 31 00:01:15.000 --> 00:01:16.005 of the corresponding app, 32 00:01:16.005 --> 00:01:19.007 and are automatically numbered, starting with one. 33 00:01:19.007 --> 00:01:22.002 Therefore, our initial migration will be named 34 00:01:22.002 --> 00:01:24.002 starting with one, and it will be stored 35 00:01:24.002 --> 00:01:27.004 in the adoption/migrations folder. 36 00:01:27.004 --> 00:01:30.003 The migrate command runs all the generated migrations 37 00:01:30.003 --> 00:01:32.004 that have not yet run. 38 00:01:32.004 --> 00:01:34.008 We can also run migrations for a specific app 39 00:01:34.008 --> 00:01:36.009 to a specific number of migration, 40 00:01:36.009 --> 00:01:40.007 by using the migrate command with an app name and a number. 41 00:01:40.007 --> 00:01:45.004 As an example, we could use migrate adoptions one 42 00:01:45.004 --> 00:01:49.006 to migrate to the first migration for the adoptions app. 43 00:01:49.006 --> 00:01:52.005 When a migration has been created, but not yet run, 44 00:01:52.005 --> 00:01:55.002 we call this an unapplied migration. 45 00:01:55.002 --> 00:01:57.006 This is a common source of errors during development, 46 00:01:57.006 --> 00:02:00.009 especially when collaborating with other developers. 47 00:02:00.009 --> 00:02:03.006 With this in mind, be sure that when working on a team, 48 00:02:03.006 --> 00:02:06.007 you coordinate carefully who is changing which model, 49 00:02:06.007 --> 00:02:08.005 and to look for new migration files 50 00:02:08.005 --> 00:02:10.009 when pulling in code changes. 51 00:02:10.009 --> 00:02:14.000 Now, let's take a moment to look at generating 52 00:02:14.000 --> 00:02:16.004 and running migrations for our Django project. 53 00:02:16.004 --> 00:02:17.005 I've opened a terminal, 54 00:02:17.005 --> 00:02:19.003 and now I'll navigate to the folder 55 00:02:19.003 --> 00:02:22.007 where the manage.py file is. 56 00:02:22.007 --> 00:02:24.009 For me, this will be on the desktop, 57 00:02:24.009 --> 00:02:27.008 then my learning Django project folder, 58 00:02:27.008 --> 00:02:29.008 and then the wisdompets folder. 59 00:02:29.008 --> 00:02:30.008 From this folder, 60 00:02:30.008 --> 00:02:33.002 I'll use ls to ensure I'm in the right place 61 00:02:33.002 --> 00:02:35.006 and that the manage.py file is here. 62 00:02:35.006 --> 00:02:38.008 Remember, on Windows, you'll need to use D-I-R. 63 00:02:38.008 --> 00:02:42.009 Now, to make our migrations, I'll type python3, 64 00:02:42.009 --> 00:02:48.001 space, manage.py, and then makemigrations, 65 00:02:48.001 --> 00:02:50.000 all one word, lower case. 66 00:02:50.000 --> 00:02:53.009 On Windows, you'll need to use python instead of phython3. 67 00:02:53.009 --> 00:02:55.004 From the output we see, 68 00:02:55.004 --> 00:02:57.004 we can see that the adoptions app 69 00:02:57.004 --> 00:03:00.009 now has migrations for the pet and vaccine models. 70 00:03:00.009 --> 00:03:02.006 The migration we just generated 71 00:03:02.006 --> 00:03:04.008 will create these models for the first time, 72 00:03:04.008 --> 00:03:07.004 so this will be an initial migration. 73 00:03:07.004 --> 00:03:09.007 Now, let's see that the migration was created 74 00:03:09.007 --> 00:03:11.006 by looking inside the adoptions folder 75 00:03:11.006 --> 00:03:14.009 and inspecting the migrations folder with ls, 76 00:03:14.009 --> 00:03:16.008 or D-I-R on Windows. 77 00:03:16.008 --> 00:03:20.003 If you want, you could also open this file in a text editor. 78 00:03:20.003 --> 00:03:26.002 I'll just type ls space adoptions/migrations. 79 00:03:26.002 --> 00:03:27.008 I can see now that there's a new file, 80 00:03:27.008 --> 00:03:32.003 and it's called 0001, underscore initial.py. 81 00:03:32.003 --> 00:03:34.006 To see which migrations exist for our app, 82 00:03:34.006 --> 00:03:36.005 and which ones have not yet run, 83 00:03:36.005 --> 00:03:39.002 I can use this show migrations command. 84 00:03:39.002 --> 00:03:44.003 So now, I'll type pyhthon3, space, manage.py, 85 00:03:44.003 --> 00:03:47.002 and showmigrations. 86 00:03:47.002 --> 00:03:49.000 There are several migrations listed, 87 00:03:49.000 --> 00:03:51.002 and they are grouped by the corresponding app name, 88 00:03:51.002 --> 00:03:52.009 alphabetically. 89 00:03:52.009 --> 00:03:55.004 Several of these are default Django apps, 90 00:03:55.004 --> 00:03:58.003 such as admin, auth and so on. 91 00:03:58.003 --> 00:04:01.007 In the second group, we can see our adoptions app. 92 00:04:01.007 --> 00:04:05.002 The default apps come with models and migrations. 93 00:04:05.002 --> 00:04:08.007 So, those migrations also appear in this list. 94 00:04:08.007 --> 00:04:11.008 The square braces on the left side of this output 95 00:04:11.008 --> 00:04:14.007 with an empty space indicates that these migrations 96 00:04:14.007 --> 00:04:16.009 have not yet been applied. 97 00:04:16.009 --> 00:04:18.004 To apply our migrations, 98 00:04:18.004 --> 00:04:23.006 we will now run phython3, manage.py and migrate. 99 00:04:23.006 --> 00:04:24.009 From the output, 100 00:04:24.009 --> 00:04:26.005 we can see that all of the migrations 101 00:04:26.005 --> 00:04:28.005 were applied successfully. 102 00:04:28.005 --> 00:04:31.004 With this in place, let's run show migrations again 103 00:04:31.004 --> 00:04:33.003 to verify our results. 104 00:04:33.003 --> 00:04:39.005 I'll type phython3, manage.py and show migrations. 105 00:04:39.005 --> 00:04:42.003 Now, the square braces on the left have an X inside, 106 00:04:42.003 --> 00:04:45.007 indicating that each migration has been applied. 107 00:04:45.007 --> 00:04:47.007 Now that we've run all of our migrations, 108 00:04:47.007 --> 00:04:51.000 let's see what the structure of our database looks like. 109 00:04:51.000 --> 00:04:53.000 I've downloaded a database browser tool 110 00:04:53.000 --> 00:04:55.001 from sqlitebrowser.org, 111 00:04:55.001 --> 00:04:56.005 which is an open source project 112 00:04:56.005 --> 00:04:58.008 that's available on most platforms. 113 00:04:58.008 --> 00:05:01.008 This tool will allow us to inspect SQLite databases 114 00:05:01.008 --> 00:05:05.005 and see their structure and contents. 115 00:05:05.005 --> 00:05:06.008 First, inside the tool, 116 00:05:06.008 --> 00:05:12.004 I'll open the db.sqlite3 file for our project. 117 00:05:12.004 --> 00:05:15.001 Since we created models called pet and vaccine 118 00:05:15.001 --> 00:05:17.000 inside of our adoptions app, 119 00:05:17.000 --> 00:05:19.005 the tables are named adoptions underscore pet, 120 00:05:19.005 --> 00:05:22.003 and adoptions underscore vaccine. 121 00:05:22.003 --> 00:05:26.000 We also have a third table called adoptions underscore pet, 122 00:05:26.000 --> 00:05:28.006 underscore vaccinations. 123 00:05:28.006 --> 00:05:32.001 This third table is used for storing the mini-to-mini field 124 00:05:32.001 --> 00:05:34.001 for pets and vaccines. 125 00:05:34.001 --> 00:05:36.006 To look inside the adoptions pet table, 126 00:05:36.006 --> 00:05:41.004 I'll click the unfold button here on the left side. 127 00:05:41.004 --> 00:05:44.005 Inside this table, we can see that there are several fields 128 00:05:44.005 --> 00:05:48.003 including ID, name, submitter and so on. 129 00:05:48.003 --> 00:05:50.006 ID is an automatically generated field 130 00:05:50.006 --> 00:05:53.006 for all tables that Django manages. 131 00:05:53.006 --> 00:05:55.003 The remaining fields match those 132 00:05:55.003 --> 00:05:57.002 that are defined in our model. 133 00:05:57.002 --> 00:05:59.007 After creating and running all of the migrations, 134 00:05:59.007 --> 00:06:02.005 we've now confirmed that they are applied successfully 135 00:06:02.005 --> 00:06:04.004 and we're ready to use our database.