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