Full Web Apps with FastAPI Transcripts
Chapter: Storing data with SQLAlchemy
Lecture: Which version of SQLAlchemy

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


Talk Python's Mastodon Michael Kennedy's Mastodon