Full Web Apps with FastAPI Transcripts
Chapter: Storing data with SQLAlchemy
Lecture: Which version of SQLAlchemy
0:00 Before we start writing code. Let's just take a really quick look at some of
0:03 the differences of the current SQLAlchemy
0:05 and where we're going and you'll see how we're gonna write code now and
0:09 then in the next chapter, we're gonna write it against the new API in
0:12 just a little bit of detail.
0:14 Here we are at SQLAlchemy. There's currently a 1.4.0b1
0:18 released about two months ago,
0:21 and this is the culmination of about 18 months of work.
0:24 And the main goal of this is to bring a new API,
0:28 which will be introduced maybe exclusively in SQLAlchemy 2.0.
0:32 So when we get to 2.0, we'll have to switch to this new API.
0:36 Let's just really quickly look at the announcements here.
0:38 So it says this is the first beta release of the SQLAlchemy 1.4 series.
0:43 It's the culmination of 18 months of effort to envision and create a modernized,
0:47 newly modernized and capable
0:49 SQLAlchemy. The first step along the way to SQLAlchemy 2.0,
0:53 and includes a gradual introduction to the simplified way of working with,
0:57 most importantly, the ORM
0:58 So if you just look at some of the changes,
1:01 there's a simple and more consistent way to work with transactions.
1:03 There's this select() construct that gets used now heavily in the ORM where it used to
1:10 be you would just go create a query
1:12 and I believe the select stuff was more in the "core",
1:14 if you wanted to do direct queries some caching,
1:17 which sounds amazing. Here's why we're having this conversation,
1:20 why this is valuable. This is the first version of SQLAlchemy to provide support
1:25 for a asyncio, meaning async and await and direct integration with FastAPI.
1:29 So that's gonna be super,
1:32 super awesome, that we can use the ORM with async and await to get
1:36 massively scalable applications all the way down to the database.
1:40 OK, so that's what the goal here is.
1:43 We're gonna go through and create everything in a familiar way,
1:45 and then we're gonna make just some changes, really only to the queries that we write,
1:49 it won't be that big of a deal,
1:50 but you know there it is. Let me just show you what some of those differences
1:54 look like. So if you go back here to the home page, real quick,
1:57 just to give you guys some guidance here,
1:59 there's this releases. Oh, look,
2:00 there's migration notes and there's a big,
2:02 long document about it. But if you look,
2:04 there's another migrating to SQLAlchemy,
2:07 which is not those migration notes.
2:08 It's a totally different one. You can see the URL appear at the top. Anyway
2:12 if you, it's really if you look at the scroll bar,
2:15 it's quite a large document. If you go down, now
2:17 what is that? About 70% of the way down.
2:20 It talks about the biggest change in SQLAlchemy
2:22 2.0 will be the use of Session.execute().
2:26 Instead of using query, you're going to create select statements and then execute them.
2:30 What's nice is down here we have a couple of examples that we could use.
2:33 So traditionally, you would write something like,
2:35 I want to get all the users, session.query(User).all(), now you
2:40 write this, you execute a select statement on the user where you get the scalars
2:44 and then you call all(). Down here you wanna do a filter.
2:49 So give me the users, so session.query(User) where the name equals "some user",
2:53 give me the first one. Now you'd write,
2:56 you would execute the statement, select user, filter by name, scalar_one(),
3:01 ok? You notice that this one applies to the query,
3:04 whereas here, your select and your query is one thing and then you execute it against
3:08 the result of the execution. So there's a little bit of difference going on here.
3:13 So you can just scroll through this and see that these APIs are
3:16 not even close to similar. Maybe there's some parts that are similar,
3:20 but to me, they feel fairly different.
3:22 Like all() is now scalars().all().
3:25 When you do a join, you have to do a unique() on it.
3:27 So there's just a lot of stuff that is really not directly transferrable. You know,
3:31 it's not necessarily obvious that that's the adjusted thing.
3:34 So what we're gonna do is we're gonna create this, this version that I believe most people
3:38 who work with the ORM of SQLAlchemy totally have down pat.
3:42 And then we're going to rewrite the queries in this,
3:44 not the classes, just the queries,
3:46 and make those asynchronous which is gonna be so worth it.
3:49 And we're gonna do that in these two chapters.
3:51 Here's the documents that you might wanna look at.
3:53 So we've got, you know,
4:00 All right, and there you can find this comparison
4:02 doc. It's about time to write some code, don't you think?