1 00:00:00,000 --> 00:00:01,500 Hello again. 2 00:00:01,500 --> 00:00:06,284 In this video, we will update entries and delete entries in our tables to see 3 00:00:06,284 --> 00:00:10,930 how these actions may change when working with relationships. 4 00:00:10,930 --> 00:00:15,332 If you closed your project, don't forget to reactivate your virtual environment and 5 00:00:15,332 --> 00:00:17,214 then pop back into the Python shell. 6 00:00:19,185 --> 00:00:23,443 And go ahead and import models. 7 00:00:23,443 --> 00:00:26,971 Let's create some variables to hold the entries we have so 8 00:00:26,971 --> 00:00:29,260 far to make things a bit easier. 9 00:00:29,260 --> 00:00:33,170 I'm gonna run a few queries that are also in the teacher's notes to speed things up. 10 00:00:34,510 --> 00:00:35,766 So lion =, and 11 00:00:35,766 --> 00:00:41,779 I'm gonna copy-paste to make this a little bit easier on myself as well. 12 00:00:44,779 --> 00:00:52,290 Models.session.query(models.Animal).filte- r(models.Animal.name=="lion"). 13 00:00:52,290 --> 00:00:56,294 And then you'll wanna put this dot first in there, otherwise it's gonna give you 14 00:00:56,294 --> 00:00:59,447 a query object instead of the actual thing you're looking for. 15 00:00:59,447 --> 00:01:02,462 So we hit that and we check lion.name, just to make sure. 16 00:01:02,462 --> 00:01:03,610 Lion, perfect. 17 00:01:03,610 --> 00:01:06,170 Okay, I'm gonna go ahead and copy-paste the rest of these. 18 00:01:06,170 --> 00:01:09,092 I'm pulling it straight from the teacher's notes just like y'all, 19 00:01:09,092 --> 00:01:10,138 just to speed things up. 20 00:01:10,138 --> 00:01:13,349 So the other animal we had was wombat. 21 00:01:13,349 --> 00:01:17,308 wombat.name, just to check, perfect. 22 00:01:17,308 --> 00:01:21,379 I'm just gonna grab the first lion log, I know that we put two in the database, but 23 00:01:21,379 --> 00:01:23,161 I'm just gonna grab the first one. 24 00:01:25,839 --> 00:01:29,220 .notes, great pouncer, perfect. 25 00:01:29,220 --> 00:01:31,641 And then the other log that we did for 26 00:01:31,641 --> 00:01:36,828 a different animal was where we did our mistake one, which was first a seal. 27 00:01:36,828 --> 00:01:42,110 So we copy-paste that one, seal_log.notes, likes to wave, perfect. 28 00:01:42,110 --> 00:01:43,030 Okay, we're all set up. 29 00:01:44,540 --> 00:01:48,850 Now that we've got our all of our variables, let's try updating some values. 30 00:01:48,850 --> 00:01:54,170 Let's change the lion's habitat from savannah to grasslands. 31 00:01:54,170 --> 00:02:00,256 I did want 100% google lion habitat to get that answer, just being [LAUGH] honest. 32 00:02:00,256 --> 00:02:08,110 So lion.habitat = "grasslands", hit Enter. 33 00:02:08,110 --> 00:02:12,501 And if I do lion.habitat, we get grasslands. 34 00:02:12,501 --> 00:02:17,473 And we can also check models.session.dirty, 35 00:02:17,473 --> 00:02:25,390 which if you don't remember, It's not callable, oops. 36 00:02:25,390 --> 00:02:30,232 It's just .dirty, there we go, which holds our change. 37 00:02:30,232 --> 00:02:35,091 The session dirty is always what holds any changes that you make until 38 00:02:35,091 --> 00:02:36,360 you commit them. 39 00:02:36,360 --> 00:02:41,026 So let's go ahead and commit that to the database, 40 00:02:41,026 --> 00:02:44,725 models.session.commit, perfect. 41 00:02:44,725 --> 00:02:50,494 And then we can check the lion_log.animal attribute, 42 00:02:50,494 --> 00:02:54,400 and it pulls up our changes as well. 43 00:02:54,400 --> 00:02:56,565 So they are reflected in the database. 44 00:02:56,565 --> 00:02:57,940 Pretty easy, right? 45 00:02:57,940 --> 00:03:02,380 It works pretty much the same as it did with a single table. 46 00:03:02,380 --> 00:03:06,653 Let's try changing our wombat animal into a seal so that our log and 47 00:03:06,653 --> 00:03:09,770 our animal actually make sense. 48 00:03:09,770 --> 00:03:15,540 So I'm gonna do seal, just so that things are a little less confusing, 49 00:03:15,540 --> 00:03:17,954 I'm gonna do seal = wombat. 50 00:03:17,954 --> 00:03:22,463 And then we can do seal.name, and you can see it says wombat, 51 00:03:22,463 --> 00:03:24,387 so we need to change that. 52 00:03:24,387 --> 00:03:28,784 seal.name = "seal", 53 00:03:31,264 --> 00:03:38,497 Ooh, not sale, seal.name = "seal", there we go. 54 00:03:38,497 --> 00:03:45,710 And seal.habitat =, "ocean". 55 00:03:48,000 --> 00:03:54,180 And now let's do seal_log.animal, and we get our changes. 56 00:03:55,540 --> 00:03:59,062 So if you see this zoo.db-journal, don't worry about it, 57 00:03:59,062 --> 00:04:02,257 it just means that things are moving with the database. 58 00:04:02,257 --> 00:04:08,182 So let's do our models.session.commit, hit Enter. 59 00:04:08,182 --> 00:04:12,420 And you can see, then it goes away, awesome. 60 00:04:12,420 --> 00:04:20,993 Lastly, let's see what happens if I change seal_log.animal_id to number 1. 61 00:04:22,740 --> 00:04:26,854 If I then do you seal_log.animal, 62 00:04:26,854 --> 00:04:33,237 which is our relationship, it's now pulling the lion. 63 00:04:33,237 --> 00:04:38,543 And if I do lion.logs, I'm getting three logs now instead of two, 64 00:04:38,543 --> 00:04:43,370 great pouncer, really likes meat, and likes to wave. 65 00:04:43,370 --> 00:04:48,797 So it's also important to know, if you change what the foreign key is, 66 00:04:48,797 --> 00:04:52,150 it will attach itself to a different entry. 67 00:04:52,150 --> 00:04:53,880 So something to keep in mind. 68 00:04:53,880 --> 00:04:59,280 I'm gonna go ahead and reset that back to what it was, 69 00:04:59,280 --> 00:05:02,769 so seal_log.animal_id = 2. 70 00:05:02,769 --> 00:05:06,760 Let's do seal.logs, perfect. 71 00:05:06,760 --> 00:05:13,758 And just to make sure, models.session.commit, Awesome. 72 00:05:13,758 --> 00:05:16,719 Now let's look at deleting some entries. 73 00:05:16,719 --> 00:05:21,969 Let's do models.session.delete(seal), 74 00:05:21,969 --> 00:05:29,270 so we're gonna delete an animal, and models.session.commit. 75 00:05:29,270 --> 00:05:35,700 With that deleted, now let's see what happens to the seal log. 76 00:05:35,700 --> 00:05:40,825 Let's do seal_log, I'm gonna print it. 77 00:05:45,520 --> 00:05:52,460 Okay, so you can see our Animal ID is now None instead of 2. 78 00:05:52,460 --> 00:05:58,461 So if you delete the animal that relates to your log, it's important to remember 79 00:05:58,461 --> 00:06:03,590 that that ID will then be switched over to None instead of an integer. 80 00:06:04,600 --> 00:06:07,150 Let's try deleting lion log. 81 00:06:07,150 --> 00:06:15,149 models.session.delete(lion_log), 82 00:06:15,149 --> 00:06:20,413 models.session.commit. 83 00:06:20,413 --> 00:06:26,830 Okay, now we can look at the logs and let's look, lion.logs. 84 00:06:26,830 --> 00:06:29,493 And we can see we only have one log now. 85 00:06:29,493 --> 00:06:31,850 The log is completely gone. 86 00:06:31,850 --> 00:06:38,370 So deleting entries will work the same way, but will also affect relationships. 87 00:06:38,370 --> 00:06:42,514 But there's also something cool that you can build into your model, 88 00:06:42,514 --> 00:06:43,949 it's called cascade. 89 00:06:43,949 --> 00:06:46,460 And we'll look into it more in the next video.