#100DaysOfWeb in Python Transcripts
Chapter: Days 33-36: Database access with SQLAlchemy
Lecture: The rest of the classes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 I told you this is the simple version
0:02 of the scooter.
0:03 What if I would like to say, run a query that says
0:07 find all of the scooters that have a low battery level
0:11 so we can go collect them and charge them up or something.
0:14 Well, we would do a query based on this, right?
0:17 How fast is that going to be?
0:19 If we have lots of scooters, it's going to be madly slow.
0:22 Why? Because it has no index.
0:24 Indexes are super, super important
0:27 for high-performance queries.
0:29 Literally adding an index if you have a lot of data
0:31 can make the query run a thousand times faster.
0:34 And it's incredibly easy to do, so let's do that here.
0:37 That was good, we're done now.
0:39 Now, queries about battery level will be way, way faster.
0:44 We also might want to ask questions
0:45 of well, how many model three scooters
0:48 edition three scooters do we have?
0:50 So if we're going to ask that question
0:52 put an index there
0:53 and we very likely want to find by VIN number.
0:56 So these are all good here.
0:58 And we could even say this has to be unique
1:02 because we can't have different scooters
1:03 with the same VIN number.
1:05 We want to add in these various constraints
1:07 and indexes and so on.
1:09 Kind of like we did for our primary key up here.
1:12 On the way primary keys automatically have indexes.
1:14 We're not quite ready to do relationships
1:16 but we'll do that in a second.
1:18 What I do want to do, is add the rest of the models
1:20 and just quickly talk through them.
1:23 So let me just drop those in here.
1:24 So here we have the location.
1:27 And for our location, same thing we have an integer.
1:29 We have a created date.
1:31 Oh, one thing, let's go back here really quick.
1:33 When we insert this
1:34 it would be nice if it automatically
1:37 just was the time when we inserted it.
1:40 Databases have this concept of a default value
1:42 if you don't specify one, use a default value.
1:45 Zero false true, or like daytime.now.
1:48 And SQLAlchemy also has this concept.
1:50 So we come over here and say the default
1:52 is going to be some function
1:54 that will be called the datetime function.
1:57 So import that, datetime.datetime.now
2:00 it's super, super easy to make a mistake here
2:03 so double-check yourself.
2:05 If I hit Enter, Python says, awesome.
2:08 We just called that function
2:09 and we're assigning the default to be well
2:12 whenever this line of code was run
2:14 which is when the app started.
2:16 That is not what we want.
2:17 We don't want to insert the scooter time
2:19 as it was created when the app started.
2:21 We would like to pass a function now.
2:24 Not the result of calling now, but the function now
2:27 which will be called any time a new record is inserted.
2:31 So this, we're going to pass now, not the value of now.
2:35 Right, so we have this default value
2:36 and let's just look through the others.
2:38 Our location has a city, street, state.
2:42 And a maximum storage.
2:44 Talk about relationships later.
2:45 Our rental, pretty similar, has a created date
2:48 but it has a start and end time
2:49 for when we actually want to rent it.
2:52 Has the user that rented it and the scooter that rented it.
2:56 And finally for users
2:57 we just have their name and email
2:59 and passwords and things like that.
3:01 Again we have some relationships.
3:02 We'll talk about them later.
3:04 In fact, let me comment out, though
3:05 all the relationships before we get into that.
3:08 Great, so besides relationships
3:11 our four main items that we're going to model are in place.
3:15 We have our scooter.
3:16 We have our users.
3:17 Users can create rentals.
3:19 Rentals apply to scooters.
3:20 Scooters are parked in locations.
3:22 Perfect, and then we have our SQLAlchemyBase here
3:24 that every one of these has to derive from.
3:29 So that rounds out the various data models
3:31 that we're going to use.