Full Web Apps with FastAPI Transcripts
Chapter: Appendix: Using SQLAlchemy
Lecture: Connecting to the database (again)
0:00 Now what we're going to need to do
0:01 is work with our SQLAlchemy engine
0:04 and the factory and the connections and all that.
0:06 And remember in order for that to work
0:07 we have to go to our db_session.
0:09 We have to initialize it.
0:11 So let me just copy over a function here.
0:13 This was just like the one we worked with before.
0:16 So we're going to import os.
0:18 import pypi.org
0:22 and we can say that's spelled correctly.
0:24 And we also want our db_session
0:29 like so. And now it has this global_init.
0:31 So, we just have to call this somewhere.
0:35 Before we try to do anything, and just to remind you
0:38 over here this sets up the connection string
0:41 initializes the engine, and most importantly
0:44 initializes this factory right here.
0:46 So, what we're going to do is
0:47 we're going to have a def main.
0:49 Here this is going to be like our main startup.
0:51 And in here we'll call global_init for the database.
0:56 And we'll use our main convention at the bottom
0:58 and just run the main function. Here we go.
1:02 I guess we can go ahead and test it and see that it runs.
1:06 Oh, it didn't like that. Let's just use
1:11 this in here we'll just go up one.
1:14 That should do. Okay, great.
1:18 So here we're connecting to final, dah dah dah.
1:20 It might even be worthwhile to say os.path.abspath.
1:30 Here we go.
1:32 Now we have the absolute path, in looks right to me.