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