Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Using our database with the SQLAlchemy ORM
Lecture: SQLAlchemy session and unit of work pattern

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The final thing to use these SQLAlchemy classes is to go through a couple of steps, create what's called an engine,
0:07 which takes a connection string. Use that to work with our SQLAlchemy Base, to bind them together.
0:13 Use the SQLAlchemy base to create the database and the tables and all those sorts
0:18 of things. This is exactly the same for every single SQLAlchemy Project you ever work on the We have a little nicer version to work with.
0:26 Let me just paste file in here that we're going to just talk about. I'm going to call it session.
0:31 Now it's going to have a single function that we call it Apps Startup, where we specify basically the connection string. We're going to use SQLlite.
0:38 You can use any database you want. POSTGRES, SQL Server, My SQL Any relational database almost is supported by SQLAlchemy,
0:47 but I don't want to have to have you set up a separate database server and
0:51 have the connection, the network connections and permissions and all that stuff lined up just right. We're going to use SQLLite,
0:57 which is a local file based database built into Python so there's nothing to run and nothing to install. So here's what we're gonna do.
1:06 Make sure we've set it up once. Make sure that we have a database file. That's the connection string for SQLAlchemy specified.
1:14 We're going to make sure the path exists, and we're gonna use path lib to say, basically, if it doesn't exist,
1:20 create the folder structure that has been passed in, before we try to save a file Their SQLLite, will create the file,
1:26 but it won't create the parent directory structure required to save that. But then we've got our connection string.
1:32 We're specifying some connection settings here, like we're going to create this thing called the Engine
1:37 This is the thing that actually talks to the various database implementations with the connection
1:42 string, you want to see the messages are SQLexchange with the database you could set
1:48 echoed a True and we're going to say we don't care about which the red stuff
1:51 is running on and they're going to create this thing called Factory. Factory is what creates
1:56 a session or a unit of work and the design pattern nomenclature. That's the thing that we're going to use each time we want to make some database
2:05 operations. We're gonna use this factory to create a session and then do all then do other work and then commit or not commit that session.
2:12 Now one of the things we have to do for SQLAlchemy to work is we have to make sure it's seen everything that derives from SQLAlchemy base.
2:21 So I'm going to come up with this little idea here of saying what we're going to do is create a single file here called this.
2:30 And in this we'll just say from db import And let's just put the things we need to put order and we're going to import user.
2:39 If we had another option, another class, we would put it here as well. Now, sometimes some tools will take this away,
2:46 especially PyCharm under certain settings. So I'm going to tell it, you know these, even though they look like they're not used this time,
2:52 this matters. What this effectively does is it loads this file and make sure that this statement is executed before we do what's next in the session.
3:01 While okay, so just that's just necessary for to actually create the tables and then over here, where it's going to go to SQLAlchemy base and say,
3:09 create all tables using this engine or this connection. Then, whenever we want to talk to the database,
3:15 we'll just call either create session or will create one of these session context things if we want to use it within a with block,
3:21 which I think is a little bit cleaner. But either way, you could use this one or this one to talk to the database.


Talk Python's Mastodon Michael Kennedy's Mastodon