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