#100DaysOfWeb in Python Transcripts
Chapter: Days 33-36: Database access with SQLAlchemy
Lecture: Creating the database schema and tables
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
The next thing we need to do in our little initialization bit. We have our connection to the database through this engine and we have
0:08
the ability to create the unit to work through the factory. What we need to do is actually initialize
0:14
the database to match exactly what we have in our models. Now this, actually, there's some non-obvious things that are going to happen here.
0:22
So what we want to do is, we're going to go down here and we're going to go to our SQLAlchemyBase. Of course, we need to import that
0:31
and it has this thing called a metadata and on there we can say create_all and pass along the engine. Okay, notice that there was no auto complete
0:38
on these parts, but that's okay. This is what we've got to type, metadata.create_all. Now, we only want to do this once this part has been run.
0:47
So we're going to not let them run that if they haven't yet. So we'll say, if not engine then raise exception you have not called global init.
0:58
So this will be totally good. It seems. Now there's one little problem here and what is it? Well, how does SQLAlchemy know what create_all means?
1:10
Create_all means, find all the classes that are derived from SQLAlchemyBase. How do we know what those are? Well, Python only knows what they are
1:20
if it's already loaded that module. So, for example, over here, if nobody has loaded up scooters or rentals those tables will not be created.
1:29
So we need a way to ensure that these are all loaded up. So there's a couple of options: we could, right here, you know, right, we could
1:37
even do it just here, 'cause it only matters when you call this, we could say, import data.models.locations. import data.models.rentals.
1:47
When you add a new one, it's pretty easy to forget, like, buried deep down in this function here that this thing is supposed to happen.
1:53
So let's do a little convention here. Create one more Python file called, __all_models. Maybe with the underscore there as well.
2:03
And the idea is with this __all_models that is where, whenever we add a new model we do that import, just like I typed.
2:10
Import data.models.rentals, locations, scooters, users. Now PyCharm is trying to be helpful and says
2:23
Guess what? You're not using this. You could delete it. if I hit Alt + Enter, it deletes it again but, no, I actually want this to happen.
2:29
This is the sole purpose of this file is just to do that import. So we could go over here to tell PyCharm Stop this.
2:38
I meant to do this, even though you think it's an issue. So now, we come over here, and just locally import data.__all_models
2:48
that way every whenever we add a new model we just put it in all models and everything will work. There's a chance it'll work if we don't do that
2:53
but there's a chance that it won't work. It depends on the order of operations and it's really frustrating and tricky
2:59
when it doesn't work, so that's why I'm making a big deal of it here. I guess while we're at it, one more thing we could do:
3:05
we really only need the SQLAlchemyBase down here if we call this as well, so we're going to do it like that. And finally, PyCharm is, again, warning us
3:15
that we're not using this, but we are. Okay, so our create tables is all ready to go. We just have to, somewhere at the beginning
3:23
of our program, called global_init, we have to call create_tables and we're off to the races.