Latest / Talk Python To Me / #344: SQLAlchemy 2.0
Transcript
- 0:00SQLAlchemy is the most widely used ORM, Object Relational Mapper, for Python developers.
- 0:05It's been around since February 2006, but we might just be in for the most significant
- 0:10release since the first one, SQLAlchemy 2.0.
- 0:13This version adds async and await support, new context manager-friendly features everywhere,
- 0:18and a unified query syntax.
- 0:21Mike Bayer is back to give us a glimpse of what's coming and why Python's database story
- 0:25is just getting stronger.
- 0:27This is Talk Python To Me, episode 344, recorded November 10th, 2021.
- 0:32Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:48This is your host, Michael Kennedy.
- 0:50Follow me on Twitter, where I'm @mkennedy, and keep up with the show and listen to past
- 0:54episodes at talkpython.fm.
- 0:56And follow the show on Twitter via at Talk Python.
- 0:59We've started streaming most of our episodes live on YouTube.
- 1:03Subscribe to our YouTube channel over at talkpython.fm/youtube to get notified about upcoming
- 1:09shows and be part of that episode.
- 1:10Mike, welcome back to Talk Python To Me.
- 1:15It's been a little while.
- 1:16Hi, how are you doing?
- 1:17Hey, I'm doing really well.
- 1:18When we last spoke, we did speak about SQLAlchemy, of course, but it was in April of 2015.
- 1:26That's been a while.
- 1:27So was that live version 1.1?
- 1:29Something like that.
- 1:31Yeah, yeah, yeah.
- 1:31And that was episode five of the podcast that's been going for six years.
- 1:35So really early days.
- 1:37I appreciate you helping me kick off the show.
- 1:39And can you believe it's still going?
- 1:40That's crazy, huh?
- 1:41Yeah.
- 1:41Doing great.
- 1:42I'm glad Python's popular enough that it can have its own podcast.
- 1:45I'm pretty psyched about that.
- 1:46I'm super psyched about that as well.
- 1:48Maybe let's sort of start with that, actually.
- 1:51Back then, Python was already popular, right?
- 1:542015, we'd already sort of hit that hockey stick growth curve that started around 2012.
- 1:59But even so, does Python's popularity and growth and appeal surprise you these days?
- 2:06No, it doesn't surprise me.
- 2:07I'm pleased that it's growing in popularity.
- 2:11I think the first thing that I saw about Python when I first got into it in the early 2000s
- 2:15was it's really clear.
- 2:16It's really unambiguous compared to everything else that I've used.
- 2:20And I always wanted to be involved in a language that a quote-unquote regular people can get
- 2:26involved with and people at a low level, high level can do things with it.
- 2:30I didn't want to be focusing on a computer language that would only be very high level
- 2:34math whizzes because I'm not a math person at all.
- 2:38That's why I don't really do Haskell.
- 2:40I wanted there to be because I worked in different jobs and I always would be, not always, but
- 2:44often someone that was writing all the frameworks and the architectures and then all the other
- 2:48people were using my frameworks.
- 2:50But a lot of those people were not at the level that I was.
- 2:54And I wanted to make sure that the stuff that I did was approachable.
- 2:57Python's very approachable.
- 2:58And the fact that now that the academic world and the media news world is getting very data
- 3:04oriented, surprise, they're all using Python.
- 3:06Yeah, yeah.
- 3:07Surprise, surprise.
- 3:08Yeah.
- 3:08I missed exactly what I would have expected.
- 3:10And of course, it might not have gone that way, but it was a pretty good bet.
- 3:13So it's cool that there's a data science profession now and Python is at the center of it.
- 3:20So I think that's really great.
- 3:22It makes a lot of sense that it's at the center of it.
- 3:24I do think it has this special appeal to people.
- 3:27And I think it's made up of a couple of parts.
- 3:29One is it's, you can be very effective with a partial understanding of Python.
- 3:33Like you could not even understand what a class is and you could still do meaningful stuff,
- 3:37right?
- 3:37Which a lot of languages don't have that feature.
- 3:39It has the packages, right?
- 3:40All the, I haven't even checked.
- 3:42Let me check it out.
- 3:43How many packages are on PyPI these days?
- 3:46338,000.
- 3:48That's a few things to just grab and, you know, build with like Lego block style, some of which
- 3:54you've contributed to up there.
- 3:55So that's pretty awesome.
- 3:56Some of the popular ones.
- 3:57Yeah.
- 3:57I think it's, I think it's fantastic.
- 3:59So good to see people continuing to embrace it.
- 4:02And you mentioned making it more accessible, right?
- 4:06Like there's different things of accessibility.
- 4:08There's, well, I can write four loops and do a list comprehension.
- 4:12And that's pretty easy to understand.
- 4:14There's another, like, I need to talk to a Postgres cluster and make it do things, right?
- 4:19It doesn't matter how simple Python is.
- 4:21When you talk to external systems, you to some degree take on the complexity of that external
- 4:27system.
- 4:27Right.
- 4:28And I do think ORMs and in the NoSQL world, ODMs, if you will, really democratize that as
- 4:36well.
- 4:36You can write the code in the language you already know, and it does the database stuff,
- 4:41including the slight mismatch of language features in SQL, like at parameter name versus
- 4:48question mark type of variations, right?
- 4:51Yeah.
- 4:52What's interesting, that particular variation is not even necessary.
- 4:57That's a quirk of the Python DB API that they decided to have different parameter styles.
- 5:01So yeah, we wanted to make it so you wouldn't have to worry about that.
- 5:04Yes.
- 5:04As far as democratizing, you know, SQLAlchemy was always aiming to democratize as much as
- 5:10it could to make things that are trivial and you shouldn't have to worry about like question
- 5:13merge versus colon name.
- 5:15You don't have to worry about that stuff.
- 5:16But at the same time, I always came from all these different database shops where we had
- 5:20to use every feature possible.
- 5:22Yeah.
- 5:23And it's always about exposing the database, the SQL.
- 5:27And SQLAlchemy remains pretty different than most tools.
- 5:30Any tool I've seen in Python, more or less, and probably tools in other languages that is
- 5:35not trying to create this abstracted way.
- 5:37You can't really know that there's some vaguely relational object store thing.
- 5:42We really want you to be in SQL and you just happen to be able to write it in terms of Python
- 5:47constructs where you can also write SQL strings.
- 5:49And we're going to try to get that to have as much flexibility as possible.
- 5:55There's a couple of interesting philosophies around SQLAlchemy or philosophies of SQLAlchemy
- 6:00that you've imbued upon it.
- 6:02And one of them is not to hide the database, right?
- 6:05Because you often end up in this leak abstraction where it's like, oh, you can just forget there's
- 6:09no database.
- 6:09Oh, wait, is it too slow?
- 6:11Well, now you're into some weird world where it's not a great fit, right?
- 6:14So maybe we could talk a bit about the philosophies a bit before we get into what's new and where
- 6:20things are going.
- 6:20Yeah.
- 6:21The basic philosophy has always been, and I think some years into it, I read some articles
- 6:25that gave me some better terminology.
- 6:26One of automation of working with a SQL database.
- 6:30Like if you're going to write an application that talks to a database and you're going to
- 6:34have 20 tables and say you're not using any ORM, you're just going to write
- 6:38where you get the rush SQL.
- 6:39You're going to find yourself writing the same insert statement, typing it out over and
- 6:44over again.
- 6:44Like I know that my table has 10 columns.
- 6:47I'm going to type this insert statement.
- 6:48That's what we call boilerplate.
- 6:49Any program that anyone writes using nothing at all, they're going to write some function
- 6:53to generate that insert statement, right?
- 6:56Like people usually talk about select statements as like the ones they want to have more control
- 6:59over.
- 7:00But there's just the boring ones.
- 7:01There's updates, inserts, and deletes, which are really boring.
- 7:03There's all the DDL for creating tables and stuff.
- 7:06That's very redundant.
- 7:08Nobody needs to, you know, if you want to learn how to do that stuff, you should learn how to
- 7:12do that stuff.
- 7:12But if you have a hundred classes, you don't want to type in a hundred create table statements.
- 7:16You don't want to have to do that.
- 7:17It should be automated.
- 7:19So the idea is that when you're working in this automated environment, you still know
- 7:23everything that's what a create table statement is, what an insert is, you know what crud somewhat
- 7:28is.
- 7:29SQL, you would know it ideally, not to say, but ideally you would know how to write this
- 7:34whole program without using anything.
- 7:35Like you would know how to use raw SQL.
- 7:37Right.
- 7:37I would say a, probably a good rule of thumb for I've studied enough SQL.
- 7:42I feel like I'm, I'm not hiding too much from myself by using an ORM would be if you could
- 7:49get the ORM to print out its statement.
- 7:51So in SQLAlchemy on the engine, if you set echo equals true, for example, and you know what
- 7:57it does when you read the statements, you're like, okay, I'm not sure I would have exactly
- 8:02seen to write the join that way, but okay, that makes sense.
- 8:05I see why it's doing, I see what this update statement, these parameters mean.
- 8:08Okay.
- 8:08Like now let's just, let's be productive.
- 8:11Right.
- 8:11What do you think about it as a rule of thumb?
- 8:12Echo equals true was when I first wrote SQLAlchemy, whenever I did for the first two weeks,
- 8:18echo equals true was right there.
- 8:20That was like the very first thing, like you will see the SQL.
- 8:23Cause I had come from using, I think I'd probably use Hibernate for a while for Java.
- 8:27Now it's 20 years later or 25, Hibernate I'm sure is very different, but at that time,
- 8:33I didn't know how to see what the heck it was sending to the database.
- 8:36I had no idea.
- 8:37I'm sure there was a way to do it, but it wasn't like obvious.
- 8:40It was like, you have to set up lockers and this and that and Java, this and Java, that.
- 8:43And the idea with Hibernate and other, it was like, you shouldn't have to know that.
- 8:48We do the SQL for you.
- 8:49Why should you know that?
- 8:50And yeah.
- 8:51Yeah.
- 8:52Use us correctly and we'll solve the problem.
- 8:54Right.
- 8:55I want to really one-to-one thing where you're writing this Python select dot where dot
- 9:00whatever.
- 9:00And it's like what you can one-to-one, if I had more videography skills and also a lot of
- 9:05time, I'd write like, I'd make some kind of cartoon that shows boom where this, how the,
- 9:10how it lines up at the statement.
- 9:11Absolutely.
- 9:12It was a, it's meant to not, it's not meant to hide anything.
- 9:15It's meant to automate.
- 9:16So if you had, it was just this term, the idea of a soda bottle company, like a soda company,
- 9:21like you're selling soda, which not, soda's not good for you.
- 9:23But if you had to bottle 4,000 bottles of soda, you would use a machine to do it.
- 9:27It's not to say that you don't know how to pour soda into a bottle yourself, but you
- 9:31need to scale it up.
- 9:32Yeah.
- 9:33It's about scaling up something that is very repetitive to be typing by hand.
- 9:37And be consistent and repetitive and whatnot.
- 9:40Absolutely.
- 9:41Absolutely.
- 9:42Yep.
- 9:42A couple of comments from the audience real quick.
- 9:44I just want to throw out there.
- 9:44D Varazo says, hello from.
- 9:47Danielle.
- 9:48Yeah.
- 9:49Yeah.
- 9:49From Psycho PG.
- 9:50Thank you for your kind wrapping.
- 9:51And Jay Lee is just cheerleading for Python, which is awesome.
- 9:55Yay for PyTox.
- 9:55Awesome.
- 9:57Hello, everyone out there.
- 9:58So yeah, I think this is really a powerful thing to think about.
- 10:02Just the reproducibility.
- 10:03And also in terms of linking into the tooling, I think one thing people often miss about the
- 10:11advantages.
- 10:11Because sometimes you'll hear people say, you should never use an RM.
- 10:14It's hiding too much from you.
- 10:16Yeah.
- 10:16Yeah.
- 10:17Yeah.
- 10:17Sure.
- 10:17Yeah.
- 10:18Sure.
- 10:18Go.
- 10:18I'm going to go do something.
- 10:20You keep working on your strings.
- 10:22So one of the things I can see that sometimes can easily get missed is refactoring tools.
- 10:29Yeah.
- 10:30Error checking.
- 10:30Right.
- 10:30If you write some kind of select statement on a class and you want to change that field
- 10:35or that column, you change the field, which obviously maps over to the column.
- 10:39But that also changes your entire code.
- 10:40If you're using a proper editor that understands refactoring, like VS Code or PyCharm or something
- 10:46like that, right?
- 10:46Yeah.
- 10:47A big thing with the refactoring and one of the things that we had to adapt to as it came
- 10:51was PEP44, which is, I call it mypy, but it's not.
- 10:55mypy is a tool that checks Python annotations.
- 10:57And we call it the mypy thing.
- 10:59It's really PEP44, which is that new thing where they're trying to have a kind of a layer
- 11:05of typing, like a strongly typed, static typed, sorry, statically typed layer on top of your
- 11:13Python script.
- 11:14So if you're a library of Python, you have to work with the system now because people expect
- 11:19it and the IDEs, I use VS Code now with, so that I keep forgetting the name of the engine,
- 11:26but that's.
- 11:27PyLance.
- 11:27You know what it is?
- 11:27Yeah, PyLance.
- 11:28I can never remember that name, PyLance.
- 11:30PyLance.
- 11:31Think of a Lance.
- 11:32PyLance.
- 11:33PyLance uses the annotations a lot.
- 11:35And I actually am seeing where it works and where it doesn't work.
- 11:39And I think the refactoring tools are greatly improved by the fact that there is this concept
- 11:44of static type annotations.
- 11:45I think there's a lot of shortcomings in PEP44.
- 11:48There's a lot of things that's like, eh, it's not great.
- 11:50I'm not 100% optimistic on it, but I definitely, it's way better than nothing.
- 11:55And there's some, also some features that are coming that are in PEPs that have not been
- 12:00implemented.
- 12:00This thing called variadic types that would allow us to actually be able to type your result
- 12:05set coming back.
- 12:06If you give it a select, we'll know the types in your result row.
- 12:09I see.
- 12:10Is that something like a generic or template type?
- 12:13It's a generic that works like the tuple type.
- 12:16Because you might notice in PEP44, you can't make your own tuple type.
- 12:20The typing that's applied to tuple is actually, if you look at the mypy source, it's hard coded.
- 12:24If equals tuple, then all these new special things.
- 12:27So things like that, they need to fix.
- 12:30Yeah, because rows from a database are essentially tuples.
- 12:32So that's how we want to type them.
- 12:34I do have a question.
- 12:34Since we're talking about the typing, I pulled up this super simple example here off of the
- 12:40SQLAlchemy documentation.
- 12:41It doesn't really matter.
- 12:42It's just a class that drives from declare to base, has a table name, and then it has the
- 12:46stuff that people probably know from many ORMs.
- 12:49These descriptors, right?
- 12:50So ID equals a column, which is of type integer, primary key equals true, name equals column, which
- 12:55is a string and so on.
- 12:56I'm going to tell you what I've been doing with this stuff lately.
- 12:59And you tell me whether this is a good idea or a bad idea or whatever.
- 13:03So what I've been doing is I've been writing this code as like this name one.
- 13:06I would say name colon string equals column string.
- 13:11So the Python code believes it's the type that's backed by the database, the column type.
- 13:16So name is a string, ID colon, and even though it's really set to a descriptor of column integer
- 13:22primary key equals true.
- 13:24Have you seen this?
- 13:25Is this a good idea?
- 13:25Should I be doing this?
- 13:26Yes.
- 13:27I have done a lot of work on this concept.
- 13:29And what you're doing is not incorrect, but there's a lot of complexities to it that require
- 13:36a little more going on.
- 13:38Right.
- 13:38And to be clear, I'm not doing this for mypy.
- 13:40I'm doing this just so my editor is more approachable to me on autocomplete.
- 13:44Yes.
- 13:44For the editor, I don't have the link handy.
- 13:46I'd have to go searching on my computer.
- 13:47So first of all, we do have a mypy plugin where you are able to use annotations like name colon
- 13:54string, and it will automatically work them into the correct kind of thing to be recognized
- 13:59also at the query level.
- 14:00It doesn't work with PyLance.
- 14:03And I have a new approach that I worked out with some other people for PyLance type stuff
- 14:08where we won't need a plugin, where you won't be able to make...
- 14:11We're not totally sure how we want to do it.
- 14:12The point is that you would say name colon.
- 14:14You wouldn't say stir.
- 14:17You would use this other construct called mapping or maps rather and maps.
- 14:21And then the map type is stir.
- 14:22The reason you do that is because if you use the user class in a query, not as an instance,
- 14:28it also has behaviors at the class level.
- 14:31Right.
- 14:31Like .desk for a descending source.
- 14:34Yeah.
- 14:35Like that kind of stuff.
- 14:35Something like that, right?
- 14:36Right.
- 14:37So I'm trying to work out the best way to do this where you're not typing the same...
- 14:42You're not repeating yourself because right now it's a little bit squirrely.
- 14:45The class works completely in PyLance.
- 14:48Everything that's expected both at the class level and at the instance level will work out.
- 14:52It probably requires that when you do the declarative class, you would use a slightly different API.
- 14:58The current proposal is instead of using uppercase column, it's this new thing called m.column.
- 15:04We can always change the names and how it looks.
- 15:06One of the flaws with that declarative model you see there is that when I came up with that
- 15:11declarative idea many years ago, those column objects are not Python descriptor compatible at all.
- 15:17They actually get replaced when you map the class.
- 15:20It's totally...
- 15:21Okay.
- 15:21...like a seal.
- 15:22It's not that clean.
- 15:24So I have ideas to make declarative cleaner and do more of what it's supposed to do without breaking the rules.
- 15:31That's the thing about Python is that as the years have gone by, everyone, even newcomers, were all much more into not into being clean about code and being more verbose.
- 15:40And typing is more verbose and asyncio is more verbose.
- 15:43When we were doing Python in 06, 07, everything was like, done.
- 15:47Just magic object.
- 15:49Boom.
- 15:49Magic object.
- 15:50Nobody wanted to type anything, which was bad.
- 15:52But we did totally because it was scripting language.
- 15:54But nowadays, people are way more tolerant of more verbosity, more clarity.
- 15:58And I'm trying to keep SQLAlchemy and my team, we're trying to keep it going along with that.
- 16:02So the PEP 44 thing, the AsyncIO stuff, that's my knowledge dump for the typing thing.
- 16:08That's fantastic.
- 16:10That's a great insight.
- 16:11It's a work in progress.
- 16:12Do you think that people are more willing to accept a little more structure in the code now because the tooling is more there to help you write it?
- 16:21Like I write my maybe C tab.
- 16:23I don't write column of whatever, right?
- 16:26Right.
- 16:26Maybe it's the tooling because nowadays people use Python data classes and they want to use Python.
- 16:32We have some support for Python data classes now.
- 16:35And the syntaxes that they want me to implement, which we've done, are pretty verbose.
- 16:40Because they want to have a data class where you have all your data class fields.
- 16:43And then within the field, they want to have the mapping information.
- 16:46And people are like, this is awesome.
- 16:47And I'm like, great.
- 16:48Because it's actually more verbose than I would prefer.
- 16:52But people are way more tolerant of that.
- 16:54So yeah, I guess their IDEs are spitting things down.
- 16:56Or just people, the values are different now.
- 16:58Kids these days.
- 16:59This portion of Talk Python To Me is brought to you by TopTal.
- 17:05Are you looking to hire a developer to work on your latest project?
- 17:10Do you need some help with rounding out that app you just can't seem to get finished?
- 17:13Maybe you're even looking to do a little consulting work of your own.
- 17:17You should give TopTal a try.
- 17:18You may know that we have mobile apps for our courses over at Talk Python on iOS and Android.
- 17:25I actually used TopTal to hire a solid developer at a fair rate to help create those mobile apps.
- 17:31It was a great experience and I can totally recommend working with them.
- 17:35I met with a specialist who helped figure out my goals and technical skills that were required for the project.
- 17:40Then they did all the work to find just the right person.
- 17:43I had short interviews with two folks.
- 17:45I hired the second one and we released our apps just two months later.
- 17:50If you'd like to do something similar, please visit talkpython.fm/TopTal and click that hire top talent button.
- 17:58It really helps support the show.
- 18:03Is the goal with the data classes to try to have a more pure entity model that doesn't have column information and key index information?
- 18:11There's some other layer that puts those together?
- 18:14I haven't really played with that aspect.
- 18:16Data classes.
- 18:17I haven't really had the Kool-Aid with the data classes yet.
- 18:21I think the idea is that it's this very clean data encapsulating object that has kind of prefab constructors and Reaper and validation.
- 18:31It does have that kind of stuff.
- 18:33That's right.
- 18:33Comparison.
- 18:34Yeah.
- 18:34I might be thinking that Pydantic takes a little further.
- 18:37Some murky stuff between data classes, Pydantic and SQLAlchemy.
- 18:41Yeah.
- 18:41They blend together in my mind a little as well.
- 18:43It's all weird.
- 18:44Yeah.
- 18:44And there's a new product called SQL Model that seems to be pretty popular in that regard.
- 18:47Yeah.
- 18:48Built on SQLAlchemy.
- 18:49Yeah.
- 18:50Yeah.
- 18:50Which is great.
- 18:51It's totally awesome.
- 18:52It's actually a more opinionated overlay of the ORM, which is also using Pydantic.
- 18:59So that's good.
- 18:59Yeah.
- 19:00The thing you've got on the screen there is another way of, yeah, that's called a...
- 19:05Yeah.
- 19:05So it's a data class, but it also has the mapper registry.mapped on it, which is interesting.
- 19:11It has some of its table metadata has not just the table name, but all the column information as well.
- 19:17It's interesting.
- 19:18It does have the same type declaration that I was trying to impose upon the traditional SQLAlchemy models.
- 19:24Yeah.
- 19:24That's one form of the data class model.
- 19:26If you scroll down, the more inline embedded one is there.
- 19:30Let me see if that's what it is.
- 19:31Yeah.
- 19:32That's the one that's even crazier, which the data class fields.
- 19:36So you're fully a data class as far as what your IDE sees.
- 19:40But then you have the SQLAlchemy mapping stuff inside.
- 19:43So all of this is...
- 19:44Well, time will tell which approach becomes the popular one.
- 19:48Yeah.
- 19:49Or people just use SQL model.
- 19:50Because the people that like data classes tend to be using SQL model at Pydantic anyway.
- 19:55Right.
- 19:56Maybe they'll just...
- 19:56They'll be there.
- 19:57I don't really know.
- 19:57Yeah.
- 19:57I'm going to talk to Sebastian Ramirez maybe in December or January about SQL model.
- 20:02Yeah.
- 20:03SQL model is interesting because it basically takes Pydantic models and SQLAlchemy and merges
- 20:10them together.
- 20:11And Sebastian has a good feel for what a nice API looks like.
- 20:15So I'm optimistic for this.
- 20:17I haven't done anything with it, but it does look pretty neat.
- 20:20What he had to do was he had to modify Pydantic's base class.
- 20:23Because when I first looked, man, maybe like in this last past year about how Pydantic do
- 20:29SQLAlchemy without having two separate models, I was like, okay, this base class has got a
- 20:33whole thing, a lot of things going on that don't work with Python descriptors.
- 20:36So it'd have to change.
- 20:37And I think Sebastian did that, basically.
- 20:39Okay.
- 20:39Went in and changed how the init works so that it's compatible.
- 20:42Yeah.
- 20:43More power to him.
- 20:44Yeah, more power to him.
- 20:45Awesome.
- 20:45I have enough to work on.
- 20:47I can't.
- 20:47This is like too much for me to also work on Pydantic.
- 20:50So I'm glad someone else did it.
- 20:52Yeah, absolutely.
- 20:52Absolutely.
- 20:53One more philosophy thing.
- 20:55That is not it.
- 20:56This is the one.
- 20:56One more philosophy thing that I want to ask about or let you just speak to, because this
- 21:01is a key element of how SQLAlchemy works.
- 21:04There's other ORMs that work this way.
- 21:06And then there's also this one, I think it's called Django.
- 21:10People might have used the Django ORM before.
- 21:12No.
- 21:13That one is more of a traditional Ruby on Rails active record where I have a thing and I call
- 21:18save on it.
- 21:19Sure.
- 21:19You went with this thing called unit of work, which is a little more transactional.
- 21:23Like I'm going to do a bunch of stuff and then all together it happens.
- 21:26Yeah.
- 21:26Maybe just speak to that real quick before we talk about it to you.
- 21:28When I was going to do SQLAlchemy, the main thing that I had worked on a lot at work
- 21:33was a lot of fancy selects.
- 21:34I hadn't done much work with the persistence side, but I read this book by Martin Fowler
- 21:39called Patterns and Enterprise Architecture.
- 21:41I read it as well.
- 21:41It was quite an interesting book back then.
- 21:43Yeah.
- 21:43And I had actually never, even though I had used Hibernate, I had never heard of unit of
- 21:47work.
- 21:47And I was like, wow, that looks cool.
- 21:50I'm going to write that.
- 21:52Because one of the philosophies was like, insert update deletes are really boring.
- 21:56And even saying update.save is really boring.
- 21:59You're going to, because I, we worked, I was working on a content management system for Major
- 22:03League Baseball.
- 22:03And when you work with CMS, you have a lot of tree-based hierarchical structures with lots
- 22:09of self-referential stuff.
- 22:10When you persist self-referential structures in the database with auto-incommented primary
- 22:14keys, you've got to get one row, get the primary key back, put it there, put it in there.
- 22:18Everything's got to be done in a very specific order.
- 22:20Yeah.
- 22:20Yeah.
- 22:21Like I tried to call save on this, but that didn't work because I needed to call save first
- 22:25there.
- 22:25And it's like, ah, this is crazy, right?
- 22:27So it's like, why would I want to do, why?
- 22:28I shouldn't have to do that either.
- 22:29I should just have this thing that like, here's everything in a transaction, just push it.
- 22:32And it, you know, took a very long time to get it right.
- 22:36It has a couple of little chinks in the armor, a couple of cases that you might have to drop
- 22:41it to call it flush explicitly, but we never get bugs with the unit of work stuff.
- 22:46If you look at unitofwork.py, it hasn't changed in years.
- 22:50Yeah.
- 22:51It took a long time in the beginning.
- 22:52And then I wrote it a few times and we thought it was really bad early on.
- 22:56It was very hard to get it right.
- 22:58Yeah.
- 22:58A lot of edge cases, right?
- 22:59I can imagine.
- 23:00People can do any sequence of things and then you have to make it right.
- 23:04Yeah.
- 23:04Mostly that we support these joint table inheritance models were very hard for me to get my head
- 23:08around how to persist that.
- 23:09And then just took a long, it just took a long time because it grew organically.
- 23:13You know, you're coding.
- 23:14You're not, you're doing.
- 23:14I'm glad you wrote that instead of me.
- 23:16So thank you.
- 23:17Yeah.
- 23:17Yeah.
- 23:18I wouldn't be able to do it today.
- 23:19You got to be deep in it, right?
- 23:21Yeah.
- 23:22Comment from Brandon Brenner.
- 23:24Hey, Brandon.
- 23:24Because I use SQL model in a demo project and it was so easy to use.
- 23:28Very nice.
- 23:29All right.
- 23:29So I think probably the big news over here is 2.0 or as you like to put it at your recent
- 23:38talk, the 1.4 inning.
- 23:39The 1.4 inning.
- 23:41Very early stages are on the site.
- 23:44In the library tab is a development thing where development docs are.
- 23:49Nice.
- 23:49And the current release is a 1.4 or whatever.
- 23:521.4.
- 23:52It has a lot of these features in there already, right?
- 23:55Yes.
- 23:56All of the, just about everything in SQL 2.0 internally is available in 1.4.
- 24:02We did the whole of the internals first and added all the stuff so that 1.4 could be a
- 24:07transitional release so that all this, all the behind the scenes would be getting tested
- 24:12and people use the new APIs and transition over.
- 24:15Is 2.0 going to drop some of the older APIs hard?
- 24:17Many.
- 24:18Yeah.
- 24:18Yeah.
- 24:19Okay.
- 24:19Yeah.
- 24:20So people who have looked, 1.4 has this big migration thing where if you're
- 24:26on 1.3, which is pretty common, you can go to 1.4 pretty easily without much problems.
- 24:30But right.
- 24:31When you're on this thing, we have this whole thing that is inspired by the Python 2.3 process
- 24:38and at the same time tries to not make some of the, what I thought were mistakes of the
- 24:44Python 2.3 process.
- 24:45So what this is based on is that 1.4 has a environment variable you can turn on in your
- 24:51console or whatever.
- 24:52A called SQL can be worn 2.0, something like that.
- 24:55And when you turn that on, you're in now, you're in now in warnings mode.
- 24:58You will get all kinds of warnings about all kinds of APIs that either have changed or are
- 25:03going away or use this one or that one.
- 25:05So a lot has happened.
- 25:06But at the same time, the reason that's maybe not as scary as you might think is that all
- 25:10the APIs that are being deprecated are APIs that I've already taken out of the docs years
- 25:14ago.
- 25:14Like the APIs that I've been telling people for years, don't do that anymore.
- 25:19We don't, we're not going to do that.
- 25:20So it's a lot of old stuff that is not what we've featured in the docs.
- 25:24Like basically the things we're taking on 2.0 are things that have annoyed me for years.
- 25:28And there's a few changes in 2.0 that are a little more boom.
- 25:31Like we changed how the engine is auto begin instead of auto commit, but it's 2.0 is basically
- 25:37going to be, I think, better.
- 25:38It's in line with this whole notion of people are, are appreciate more explicitness when things
- 25:43are clear.
- 25:44So 2.0 is going to remove a lot of implicit stuff, a lot of five ways to do the same thing
- 25:50patterns.
- 25:50It's going to narrow you down into one or two possible patterns for things.
- 25:54Yeah.
- 25:55That hasn't been a thing with SQLAlchemy, right?
- 25:56There's a lot of ways to accomplish stuff.
- 25:59Yeah.
- 25:59Yeah.
- 25:59Because I came from Perl.
- 26:00I can't, I'm sorry.
- 26:01I was doing Perl.
- 26:03It's still the case that there's more than one way to do that.
- 26:06You can't really get away from that totally.
- 26:07Well, there's often there's the, I want the super simple, easy way, just call an ORM method
- 26:13and then, oh, we got to rewrite the select statement to use the store procedure type.
- 26:18There's usually a backdoor type of thing in a database world that has to happen at some
- 26:22point, right?
- 26:22Yeah.
- 26:23Store procedure is a pretty dramatic example.
- 26:26But yeah, these days the way is that when you write your code, it's going to be clear
- 26:29where the SQL is being executed, where the IO happens.
- 26:34In many ways, it actually was inspired by AsyncIO, where I had been tweeting a lot with people
- 26:39into AsyncIO, which I was a little skeptical of it.
- 26:42At the same time, what people appreciate about it is that it can show, you're very aware of
- 26:46where the actual message to the database is happening.
- 26:50This is where the message gets sent down.
- 26:51Here's where it comes back.
- 26:52When you say await, now.
- 26:54Yeah.
- 26:55Now it's happening.
- 26:56Now, I think that's a little heavy handed, but at the same time, I appreciated that notion.
- 27:00So with 2.0, I tried to work with that idea that we want to make it very clear.
- 27:03Here's where you're making a SQL save it.
- 27:06Here's where it's executing.
- 27:07Here's where the results are.
- 27:08And then also, here's where the transaction is.
- 27:11Those are actually the biggest changes that you're going to know that you're in a transaction.
- 27:15You're going to know when your transaction ended.
- 27:17You're going to know if you committed it or if it just rolled back.
- 27:20Yeah.
- 27:20And you'll know.
- 27:21And the code is going to be a little more verbose than SQL Alchemist here at .1.
- 27:25But people are already writing code that way.
- 27:27Because now that I've seen many years of people writing code, you know.
- 27:30They're like, I really want to know when this happens.
- 27:31So I'm going to be super explicit about it.
- 27:33People don't want, they don't want excess bells and switches that don't seem to have any purpose.
- 27:39But if your code is clear, basically being able to read the code and the intent is as clear as you can get it.
- 27:47Without, this just does that.
- 27:48We just know it does that.
- 27:50That's where I'm trying to go.
- 27:52And I'm somewhat freed by the fact that I know people are more tolerant of step A, step B, step C.
- 27:58Sure.
- 27:58A little less magic, yeah.
- 28:00Yeah.
- 28:00People are okay with it.
- 28:01Now, they weren't okay with that tiptoe 12 years ago.
- 28:03People are like, that's too much typing.
- 28:05Sure.
- 28:05So it's good.
- 28:06I want to talk about this migration thing, like what a person does to go from a 1.3 application onward.
- 28:11But I think that's getting a little bit ahead of ourselves.
- 28:14What about we talk a little bit about, you know, what is, what are the major features coming in 2.0?
- 28:20First of all, obviously, we're finally to Python 3 only.
- 28:23Yeah.
- 28:24What is it?
- 28:253.6?
- 28:26That you're, has a minimum version?
- 28:27We're going to make it 3.7.
- 28:283.7.
- 28:28Okay.
- 28:293.7.
- 28:30Yeah.
- 28:303.6 is EOL.
- 28:31So.
- 28:31Yeah.
- 28:32In a case of a month or two.
- 28:33Yeah.
- 28:33Yeah.
- 28:33No point to do that.
- 28:34Yeah.
- 28:34Just today, or I didn't merge it yet, but like we're ready to merge this gigantic Garrett
- 28:39review that's going to take all the Unicode conversion crap out.
- 28:43That was all Python 2.
- 28:45Python 3 having native Unicode is tremendous for us because all the DB APIs do the Unicode now.
- 28:52It was the new API.
- 28:53So yeah, we, we have this new school caching system that I've talked about a bunch in the,
- 29:00some of the talks I did recently.
- 29:01Yeah.
- 29:02Tell us about that a little bit.
- 29:03Yeah.
- 29:03That's in 1.4.
- 29:04Is that like compiling the SQL statements and then like caching those results or what are
- 29:10we talking about here?
- 29:10It caches the, so when you run a SQLAlchemy statement, we have to take your Python code and
- 29:15make a string out of it, which is the string we sent to the database.
- 29:18We also have to look at that thing and figure out what kinds of results are we going to get
- 29:21back?
- 29:22Are we going to get strings and dates, injures and floats?
- 29:25And then for those strings and states and floats and whatever, do we have to do any processing
- 29:29on the rows?
- 29:29We want to have that all set up because if you get 10,000 rows back, you want to make all
- 29:33those decisions up front.
- 29:34So that when you run 10,000 times, everything is as fast as possible.
- 29:39Everything is already figured out.
- 29:40When you do the ORM, that whole process becomes like way more complicated.
- 29:44There's way more going on as eager loaders and there's more fancy kinds of types.
- 29:47We might be taking columns and putting them into an object.
- 29:51All of that stuff is time consuming.
- 29:54And it all now in 1.4 lives behind what I call the cache wall, which means when the
- 29:59caching is working, which it seems to work, it's only going to do that stuff once for a
- 30:04statement that's been cached.
- 30:05So when we run your statement, we're going to do a process that's still a little pricey,
- 30:09which is to get a cache key from what you typed.
- 30:12You've got to determine some sort of unique hash type of thing out of it.
- 30:15It's a unique, it's actually a gigantic tuple right now.
- 30:18And maybe it'll be a hash in some other release.
- 30:20But for now, it's a giant tuple because Python's pretty good at that.
- 30:22It's cheaper than running, than doing the whole compilation.
- 30:24And then we get the whole compilation from a cache if it's there.
- 30:28And it's LRU cache.
- 30:29So if it's not, we make it.
- 30:30And it allows us to put more bells and whistles into the compiler.
- 30:35We have this thing where it will now detect if your statement will produce Cartesian product,
- 30:40meaning you have joins between tables that are not linked together.
- 30:43And we have all kinds of other fun things the ORM can do that take a little more time to compile.
- 30:48But now they're behind a cache.
- 30:49So the performance is not really impacted too much.
- 30:53And so that's exciting stuff.
- 30:55And we're going to be looking at 2.0 to start using Cython to speed this up even more.
- 31:00Oh, fantastic.
- 31:01Yeah, it has C-Extensions that we wrote as C code years ago.
- 31:06We're going to migrate those to Cython so that we can more quickly add new Cythonized code
- 31:12for different sections to speed up to how those cache keys get built.
- 31:15Yeah, it's one thing to say we're going to write some big chunk in a C layer.
- 31:19It's another to say just that loop.
- 31:21Could that loop be C, right?
- 31:24And with Cython, you can kind of just do that, right?
- 31:27You can do that.
- 31:27You could do it quicker and without having to worry about all the memory reference counters.
- 31:32C code in Python is pretty tedious.
- 31:35I'm not really a spectacular C programmer anyway.
- 31:38Well, and also a lot of people who might want to come contribute
- 31:40are probably not C developers.
- 31:42They're Python developers first.
- 31:43Yeah.
- 31:44Yeah, Cython is pretty cool.
- 31:45Cython is pretty cool.
- 31:47Because you can really, if you just do Python, you can do it.
- 31:49It's just like a strict Python.
- 31:51Yeah.
- 31:51It's pretty neat.
- 31:52Yeah, it is.
- 31:52And we found that some of the Cythonized versions of the functions
- 31:56are actually faster than RC code.
- 31:58Really?
- 31:58Which seems strange.
- 31:59That's pretty strange.
- 32:00Yeah, faster.
- 32:01Because Cython, the people that did Cython optimized the crap out of it.
- 32:05They know all these very esoteric Python C API things that, like literally, we have RC code
- 32:11is not a lot of codes.
- 32:12That's like a few hundred lines.
- 32:13We'll call it a tuple get item or whatever it's called.
- 32:16Cython has some, if we do this, then it's faster.
- 32:19And it actually is.
- 32:21I can't give you detail on, because I don't know, but like we did benching and Cython
- 32:26was actually faster than RC code.
- 32:27Yeah.
- 32:27I remember way, way back when hearing, you know, people used to say, well, if you want
- 32:33it to be fast, you have to write an assembler.
- 32:34And then you can use C if you're not, if it's not going to be that fast, you can just use C,
- 32:38right?
- 32:39That's a different kind of fast.
- 32:40Right.
- 32:40Well, what I was.
- 32:41To be clear.
- 32:41What turned, of course, but to be, well, the interesting thing to me was there was this
- 32:46switch when the compilers got good enough that you probably were slower writing
- 32:50assembly for most people.
- 32:52Right.
- 32:52And I feel like the thing you're describing here is kind of like that transformation like
- 32:56this.
- 32:56The compiler understands the whole system better than if you were to try to do it yourself
- 33:01at the same level.
- 33:02They found.
- 33:02Yeah.
- 33:02They've apparently.
- 33:03Yeah.
- 33:04I don't know the specifics, but in some cases they've had figured out ways to make it even
- 33:08faster.
- 33:08So there's no reason to not use Cython.
- 33:10Yeah.
- 33:10You think of what people are doing with Cython, right?
- 33:12Like they're running on supercomputers doing huge calculations that take.
- 33:17So they have a strong motivation to make it a little bit faster here and there.
- 33:20Right.
- 33:21Yeah.
- 33:21Look, here's the thing about Python.
- 33:22Python is a high level scripting language.
- 33:24It's interpreted always.
- 33:26So it's already not.
- 33:28I don't want to say that, but if you're really writing high frequency training software, you
- 33:33probably want to use something like Rust or something.
- 33:36Python's not really that.
- 33:38It's a lot of things, but it's not high frequency training speed demon type that it's just it's
- 33:44right.
- 33:44You can make it do that.
- 33:45But once you start wanting it to be sub millisecond, you might want to start.
- 33:51But other than that, you're probably fine.
- 33:52Yeah.
- 33:52Yeah.
- 33:53It's not going to.
- 33:54I might be wrong.
- 33:55It's not going to control the rockets five, but I guess maybe it does.
- 33:58I don't know.
- 33:59Yeah.
- 33:59It's have to be that.
- 34:00It's so useful for so much stuff that.
- 34:02Yeah, absolutely.
- 34:03Okay.
- 34:04So that's really cool.
- 34:05So you're thinking about switching some of your C code over to Cython, which is really
- 34:10interesting.
- 34:10And I didn't didn't see that one coming.
- 34:13That's awesome.
- 34:13The cache layer is cool.
- 34:15So that way, if I call a function and in that function, I do a query statement in the ORM.
- 34:21The first time that's expensive.
- 34:22The second time it's not free, but less expensive.
- 34:25Right.
- 34:25Cheap ish.
- 34:26Much less expensive.
- 34:27Much less.
- 34:27Okay.
- 34:28Yeah.
- 34:28Yeah.
- 34:29We haven't sped up yet.
- 34:30And I've been speaking this for many years is when you fetch rows from the ORM to create
- 34:35the Python objects that are your classes, that's still more expensive than we'd like
- 34:40it to be.
- 34:40Yeah.
- 34:40That's super expensive in terms in ORMs and ODMs.
- 34:44If you select 10,000 rows out of a database, it's probably the serialization or deserialization.
- 34:49That's the cost, right?
- 34:50Completely.
- 34:50Okay.
- 34:51And we've made, I've made the loading way.
- 34:54I spent years and years making it way faster by doing that whole thing where we pre-calculate
- 34:59everything up front.
- 35:00Like every, every like loader, we're not going to like every time you get a row, it's
- 35:04like zip, zip, zip, zip, zip.
- 35:05We have these little callables set up.
- 35:06But still just to make the object, just to make a new class in Python is expensive.
- 35:11It's pricey.
- 35:12So still a brick of progress.
- 35:13But yeah, the caching will not speed that up.
- 35:16It'll speed up the overhead per query.
- 35:18Which is still like every, every area you can speed it up is great.
- 35:21Danielle out there says, Cython is also a great choice to forget about ref count and
- 35:26Psycho PG2.
- 35:27It took a while to find all the smaller leaks.
- 35:30Yeah.
- 35:30Ref counts are tough.
- 35:31Ref counts are tough.
- 35:33Yeah.
- 35:33I'm pretty proud of myself that I did figure them out to some degree.
- 35:36They're not as hard as Malik and Free, but the ref, it's an esoteric set of rules to
- 35:41ref counts.
- 35:42While we're down in the internals, let me ask you this.
- 35:43If you've thought about this or experimented with this, any.
- 35:46One thing as I dug into Python's memory model that I found to be really interesting is when
- 35:55you create, you know, you have GC, the GC module, you can say GC get thresholds or set thresholds.
- 36:00And the thresholds tell you what will trigger a garbage collection, not reference counting,
- 36:04but a cycle detection type of thing.
- 36:07Right.
- 36:07Yeah.
- 36:07The defaults, at least last I'd looked, I haven't looked in 3.10, but in 3.9, it was
- 36:12700, 10, 10, which means 700 allocations.
- 36:17And then for every 10 Gen 0 collections, there's a Gen 1 and every 10 Gen 1, there's a Gen 2
- 36:22collection.
- 36:23That 700 means if you allocate more, 700 more classes or dictionaries or tuples or whatever
- 36:31than have been freed, a GC will run.
- 36:34Yeah.
- 36:34So if you select 10,000 rows out of a database, how many GCs are running?
- 36:39I mean, that's a lot, right?
- 36:41That's like 14 GCs.
- 36:43If you're taking the 10,000 rows of putting them in a gigantic list, then it's not GCing much
- 36:46at all because you're putting it all in a gigantic buffer.
- 36:48You're going to have the size of your process growth.
- 36:51But the, I mean, you're not freeing any, but the, and you're not ref count freeing any either,
- 36:55but you're, you're allocating 700 more.
- 36:57Yeah.
- 36:58I think that triggers, I think that might trigger 14 GCs, unless I'm understanding it wrong.
- 37:02You would know, I don't know anything about, I wouldn't know.
- 37:05I know, all I know about GC is that it's going to happen.
- 37:07Yeah.
- 37:08I mean, the reference counting stuff is super straightforward, but the GC side, so I've always thought
- 37:13about this around the database.
- 37:15Even if you're just getting dictionaries, not ORM classes back.
- 37:19Yeah.
- 37:19If you're selecting a whole bunch of stuff, not only are you doing that serialization layer,
- 37:23you're also incurring a bunch of GC because it's trying to preemptively run around and look
- 37:27for cycles that might've been forming.
- 37:29One thing you can do is make the code so that you don't have as many cycles.
- 37:32And we actually do that.
- 37:33We've had people post issues related to this.
- 37:36So I said, I don't know much about GC.
- 37:38I do know that if you can reduce reference cycles, you will have less of these asynchronous
- 37:42GC runs happening.
- 37:43So we had someone specifically came to me with a whole lot of use cases where he showed when
- 37:48you run this little code, like all these GCs would happen.
- 37:50And we went in there and got rid of a lot of cycles.
- 37:53So we actually have a test suite in test slash AA profiling test man usage called cycle test,
- 37:59something like that.
- 37:59Okay.
- 38:00And they're all these little whiz bangs that run, make a session and close it, do this,
- 38:04then that, do this and that.
- 38:05And then it will runs it in a harness that actually counting what GC is doing.
- 38:09And it's asserting that there's only five GC calls and not 20.
- 38:12Right.
- 38:12Yeah.
- 38:13That's fantastic.
- 38:13We have a lot of that happening.
- 38:15Yeah.
- 38:15Interesting.
- 38:15Yeah.
- 38:16It's cool that you're, you're thinking about it.
- 38:18I mean, on one hand, this is an external thing.
- 38:20So you can control your data structures, but you can't totally control what people set
- 38:23their GC to do.
- 38:24But anyway, it's just something that I always think about when, when you're creating lots
- 38:28of these ORM things, because a lot of the signals to the GC are like, oh, I got to get
- 38:32busy.
- 38:32But yeah, you're not even like, that's a single call to SQLAlchemy and I'm just waiting.
- 38:37There's nothing else I can do to get out of the way.
- 38:39You know what I mean?
- 38:39We've just been around for so many years and people have come to us.
- 38:42So we have all kinds of man usage and C profile stuff at our test suite to make sure this
- 38:47does the call care of Stoke Grove here to make sure we don't do too many GCs.
- 38:50We try to work on that as much as we can.
- 38:52We have a new course over at Talk Python, HTMX plus Flask, modern Python web apps hold the
- 39:00JavaScript.
- 39:00HTMX is one of the hottest properties in web development today.
- 39:04And for good reason.
- 39:05You might even remember all the stuff we talked about with Carson Gross back on episode 321.
- 39:10HTMX, along with the libraries and techniques we introduced in our new course, will have you
- 39:15writing the best Python web apps you've ever written?
- 39:18Clean, fast, and interactive, all without that front-end overhead.
- 39:21If you're a Python web developer that has wanted to build more dynamic, interactive apps, but
- 39:26don't want to or can't write a significant portion of your app in rich front-end JavaScript frameworks,
- 39:31you'll absolutely love HTMX.
- 39:34Check it out over at talkpython.fm/HTMX or just click the link in your podcast player show notes.
- 39:41I would say one of the things that makes me more excited than most of the other stuff here
- 39:46is probably the async support that you have going on.
- 39:50You want to talk a bit about that?
- 39:52Yeah.
- 39:52So when I was doing 2.0, I thought I wanted to be compatible with how async.io works because
- 39:58I figured I would eventually do an async.io API for it.
- 40:01But then I suddenly had this idea, why don't we just do the async.io thing?
- 40:05And we do it doing this, we use this library called Greenlit.
- 40:09The way the async.io works is that SQLAlchemy has async blocking API.
- 40:14Yeah.
- 40:14We call dbapi execute, it blocks, it comes back.
- 40:17Usually the way you want to make that kind of thing work in an async non-blocking context
- 40:22is you have to throw all the stuff into a thread pool.
- 40:25You probably don't want to be in the wiki.
- 40:26This is more old stuff.
- 40:28Okay.
- 40:29Whoops.
- 40:29Yeah.
- 40:29Like that one.
- 40:30Click an async ORM at the top.
- 40:33There we go.
- 40:34This is the one I was looking for.
- 40:35That's the most basic.
- 40:35This is the one I wanted to show people.
- 40:37Yes.
- 40:37Not the session one, but the ORM.
- 40:40Yeah.
- 40:41This is ORM.
- 40:41So what's important here is that the notion that we, 2.0 style has the thing where there's
- 40:47a very specific place that execute happens.
- 40:49Because with the query object that SQLAlchemy session.query, you could say query.all, query.one,
- 40:56query.first.
- 40:56I didn't want to have all this await this, await that, await everything.
- 40:59With the new API, it's just, which is really the old API.
- 41:03It's await execute.
- 41:04And then you get your result back and then the result is buffered.
- 41:07And the way this works that changed the whole asyncio equation for SQLAlchemy is that this
- 41:13is not a rewrite of anything.
- 41:14This is a layer on top of the completely blocking API stuff.
- 41:18And it does not use threads.
- 41:20It uses Greenlit.
- 41:21Right.
- 41:22When I went to look at what Greenlit actually does, Greenlit is compared to a thread, but
- 41:27it's like a thread.
- 41:28But when you see what it is, it doesn't really feel like a thread because it's like, it's
- 41:31a little thing with code that you can context switch somewhere else.
- 41:34More like, it's more like generators than it is like threads.
- 41:37Like the weirdness of generators skipping around.
- 41:39Yeah.
- 41:39Exactly.
- 41:39So the way Python asyncio has that await keyword, the await is your context switch in asyncio.
- 41:45Now, I wanted the internals of SQLAlchemy to work in this context switching way, but to
- 41:50integrate with await.
- 41:51So we basically made a Greenlit wrapper that emulates await without actually using await.
- 41:57So you're able to take SQLAlchemy's blocking internals, which ultimately go out to, when
- 42:03you use SQLAlchemy asyncio, if you're going to use a DB API, actually it's not DB, it's
- 42:08a database driver, that is also asyncio.
- 42:11Currently, here goes the goal.
- 42:13Async PG, AO SQLite, there's async my, and also there's one, oh, I can't remember the name
- 42:19of it.
- 42:19Wait, I can't remember the name, trying to get Danielle to, Psycho PG3.
- 42:23I was teasing Danielle because he's on there.
- 42:26There's going to be a Psycho PG3 that we're also going to support that does asyncio for
- 42:31Postgres.
- 42:31So you have asyncio driver, SQLAlchemy then adapts, it's the await calls into the synchronous
- 42:38thing using the Greenlit thing.
- 42:40And then on the outside, where you see on the screen there, is you have asyncio stuff.
- 42:45And when I actually talked to some of the people that were involved with asyncio in Python,
- 42:49they explained to me, you could do it the other way too.
- 42:51You can have a synchronous API that calls into an async middle that goes back out to a
- 42:57sync API.
- 42:58And that's actually easier to do.
- 42:59But since SQLAlchemy was already written in sync blocking style, we kept it that way.
- 43:02Right.
- 43:02It's a lot easier to wrap an async shell on something that's 15 years old and polished.
- 43:08Right.
- 43:08Then to say, we're going to do the inside all over again.
- 43:11Yeah.
- 43:11Yeah.
- 43:11And the thing with async is that there were some SQLAlchemy based things on GitHub and
- 43:16whatever that were basically taking our engine and connection, rewriting them as totally brand
- 43:20new asyncio things.
- 43:21Yeah.
- 43:21And I always thought that's not, you can't maintain that.
- 43:24It's protecting our code, taking about 30% of what it actually does and putting it up.
- 43:29And I was like, that can't work.
- 43:31There's got to be a way to get green.
- 43:33Cause I had used G events and event lit a lot.
- 43:36And I'm like, okay, I know if I'm going to do this.
- 43:38I know it can context switch without using any way.
- 43:40I know it can do it.
- 43:41I just had to go in agreement and read the docs.
- 43:44And I did that.
- 43:44Yeah.
- 43:45That's really cool.
- 43:46I'm super excited to see this because it opens up using SQLAlchemy with some of the other
- 43:51frameworks that really leverage it.
- 43:52For example, FastAPI, but there's plenty of others as well.
- 43:56Right.
- 43:56Yeah.
- 43:56Another cool thing about asyncio was that when I did the SQLAlchemy 1.4, which introduces
- 44:02this new query ORM interface, but keeps the old one, which is not going to go away.
- 44:07Cause there's just too much code in the old query interface.
- 44:09But for asyncio, it's totally brand new greenfield development.
- 44:12So I said with async, you've got to use our new API that automatically got a lot of people
- 44:16to be testing the new way of working and got us to get people.
- 44:20Cause the one thing that makes libraries really good is when a lot of people use them and finds
- 44:25problems with them and add use cases and the library becomes mature.
- 44:29So the asyncio thing has been enormously helpful to bring a lot of people to SQLAlchemy
- 44:33who would not have been there.
- 44:35A lot of people who were probably going to leave SQLAlchemy stayed.
- 44:37Yeah.
- 44:38And people using the new 2.0 thing.
- 44:40SQL model probably wouldn't have been based on SQLAlchemy.
- 44:42Not at all.
- 44:42Yeah.
- 44:43Yeah, exactly.
- 44:45Because obviously, Sebastian needs that to integrate with FastAPI.
- 44:48And the async is a core element there.
- 44:50Audience, Daniel says, I wonder if other drivers are moving to expose native async interfaces
- 44:55themselves.
- 44:56Time to go back to DBC.
- 44:57DBC, yeah.
- 44:58That's the database special interest group.
- 45:00Sure.
- 45:01Daniel can go there.
- 45:03I've not had much success when I go to the SAC, suggest things.
- 45:07Yeah.
- 45:08Let me talk about this async API a little bit for folks who are probably familiar with SQL
- 45:13Alchemy's traditional API.
- 45:15because there's a couple of things that stand out to me here.
- 45:19One is, let's start simple here.
- 45:22One is you have this concept of creating a select statement and then executing the statement
- 45:28as opposed to like a query of a dot filter.
- 45:32So there's this sort of this statement model that you build up.
- 45:37Yeah.
- 45:37You want to speak about that?
- 45:38The original, the SQLAlchemy with query really is 95% the same statement model.
- 45:44It's just the statement has the fetcher, the getter, the result fetching method stuck on
- 45:49it.
- 45:49And when we, when query came out, that was a historical thing.
- 45:54The original idea of SQLAlchemy was that there was only going to be the select object.
- 45:58And then you would use select object in with the session to get results.
- 46:02And then it wasn't really very done very well.
- 46:05It wasn't very flexible.
- 46:06So people were coming to me with, why don't we use generative builder thing?
- 46:10And that's what happened.
- 46:12Cause I wasn't really, I was kind of, I was a little bit rudderless in those days.
- 46:15So we had query and it took me quite a long time to realize, wait a second, query is basically
- 46:21once query got much more fancy, it started as very simplistic, well, I could query for one
- 46:24object and do this and that.
- 46:25As I had to keep adding to it, it became clear that I was rebuilding select.
- 46:29They met in the middle.
- 46:31Yeah.
- 46:31Then like, I'm just rebuilding select in exactly the same, but slightly different way.
- 46:35And that bothered me for years.
- 46:37So with 2.0, I finally took that on and did a really long, difficult refactoring to take
- 46:45all of the guts of ORM query and put it into a different module called context that you don't
- 46:50deal with.
- 46:51And when you get the select object you see there, that indicates the exact same intent as query
- 46:57a options filter.
- 46:59And they both go to the same backend that does all the ORM figure it out stuff.
- 47:04Right.
- 47:04That's a little more elaborate to come up with the sequel that will be sent to the database.
- 47:08One of the biggest refactoring was I've ever done.
- 47:11And it was quite stressful when it seemed like I'm sitting dead ends is, oh, this is not going
- 47:14to work.
- 47:15Yeah.
- 47:15I can imagine.
- 47:16It was tough, but.
- 47:16Yeah.
- 47:17This is cool.
- 47:17Yeah.
- 47:18So basically the query of class dot that style is going away, right?
- 47:23It's never going to go away because there's so much code in that style that it's basically
- 47:27not, when you go to the website, it's not going to be in the tutorial.
- 47:30It's going to, it'll be in reference docs.
- 47:32So people that deal with that code could deal with it.
- 47:34It'd be in some sort of middle ground where it's not fully deprecated, but it's not promoted
- 47:38either.
- 47:38Huh?
- 47:38It's called legacy.
- 47:39We've had different systems that stayed legacy for many years.
- 47:42Makes sense.
- 47:42It's very.
- 47:43There's so much code written that way.
- 47:45I totally agree.
- 47:45Yeah.
- 47:46I probably would never take it out.
- 47:47So, but what I did do is when I do that, I try to make that code written such that it's
- 47:52all by itself.
- 47:53And it basically what query does now is it makes that select inside and sends it off.
- 47:58So it's what you call eating your own dog food.
- 48:00It means that the alternate API uses the real API internally.
- 48:05So you're testing it either way.
- 48:06Right.
- 48:06Going back to our patterns talk, it's like an adapter for the select pattern.
- 48:11Yeah.
- 48:11Okay.
- 48:11API.
- 48:12Yeah.
- 48:12Two other things that stand out here.
- 48:14One is you create a session maker traditionally, and you create a session by calling it.
- 48:19Here you have an async session maker, right?
- 48:22To create them.
- 48:23That's one of the differences.
- 48:24That's not super different.
- 48:26But the other one that is interesting to me is now these things are context managers, which
- 48:32when I used to work with SQLAlchemy, traditional ones, I would create a class called a session
- 48:38context just so I could do this in a with statement.
- 48:41And here, obviously it's an async with statement, but that's just the async.
- 48:44It's in the async aspect of it.
- 48:46It's in the async aspect too.
- 48:47Yeah.
- 48:47I should mention that when SQLAlchemy in 2006, we were in Python 2.3, there were not context
- 48:53managers.
- 48:54They didn't come out until Python 2.5.
- 48:55Right.
- 48:55So you can't integrate with them if they don't exist in your API, right?
- 48:58So I didn't.
- 48:59So nothing was based on that.
- 49:00SQL me 1.4 and 2.0, as much as possible, completely support the context manager model.
- 49:06And the docs and the tutorials, if you look at the new 1.4 tutorial that's on the site, it's
- 49:12all about context managers.
- 49:13And all the try, accepts, and ready your own thing should go away.
- 49:17You should use context managers now.
- 49:19And what you see there with the async pattern, I've made it so that if you're using all the
- 49:23new APIs, the context manager pattern with the engine and connection in core is mirrored to
- 49:30the context manager pattern with the session and the session maker.
- 49:32Nice.
- 49:33And then it's also mirrored in the asyncio API.
- 49:36So there's four different APIs.
- 49:37There's core and ORM, sync and async.
- 49:39And they all should have, as much as they can, the same, this means cone.
- 49:45Yeah.
- 49:45They have the same, as much as possible, they share the same context manager patterns.
- 49:50And we want you to use context managers for everything now, for any kind of blocking.
- 49:54And that's also how we can improve the transactional model so that you're really working the transaction
- 49:59all the time with the core.
- 50:01Because it's not a burden because you have context managers.
- 50:04Things I see here.
- 50:04I want to ask you about two bits of code and then take another question from the audience.
- 50:08So you have a with session.begin and you do some inserts.
- 50:13And then you don't say session.commit.
- 50:15So the way this works, I'm presuming, tell me if I understand this right, is if it makes
- 50:20it through the with block without an error, it commits.
- 50:23If it makes it through the with block with an exception, it just doesn't commit and effectively
- 50:28rolls it back.
- 50:28That's what it does.
- 50:29It does a rollback.
- 50:30Yeah.
- 50:31That's correct.
- 50:31Yeah.
- 50:31And we've had the begin context manager for a long time.
- 50:35It's just, it wasn't totally consistent everywhere.
- 50:38But it's always, we've had that for a long time.
- 50:40Yeah.
- 50:40But now I've made sure that all the opening and closing of a resource and the begin commit
- 50:45of a resource are consistent.
- 50:47And also there's always a way that you can open the resource and begin commit in one line.
- 50:53Also the session maker has a begin on it now.
- 50:56Super.
- 50:56And you could do that.
- 50:57The other thing I'm super familiar with execute, but then you also have stream, which looks
- 51:02real similar.
- 51:02What's the difference there?
- 51:04Stream is asyncio only right now.
- 51:06Stream is because it's important in many cases to get a result that's doing async streaming.
- 51:11And when you're doing async streaming with a, you've got to have the await keyword.
- 51:15So you can see the result object returned from session.stream is called an async result.
- 51:19And all of the methods have the async annotation on them so that you see it's async for a in result.scalers.
- 51:26Oh, I see.
- 51:27So the things you interact with, the subsequent functions are themselves all async as well.
- 51:31Okay.
- 51:32Yeah.
- 51:32That's cool.
- 51:32Yes.
- 51:32And what that will do is that will try to use a server-side cursor.
- 51:36Yeah.
- 51:37Yeah.
- 51:37That's what I imagine.
- 51:37That's cool.
- 51:38You actually are.
- 51:39Instead of trying to pull it all into memory, if you got a thousand rows back, you could start
- 51:43pulling them more generator-like, right?
- 51:45When you use a server-side cursor, you actually are genuinely not pulling everything into memory
- 51:48first.
- 51:49When you use the buffered result, you're getting everything into memory.
- 51:52Yeah.
- 51:52Which is normally okay.
- 51:53Yeah.
- 51:54Most of the time it's fine.
- 51:55But if you want a whole bunch, oh, you're flowing it on.
- 51:58Yeah.
- 51:58Anyway, a lot of cool stuff there.
- 52:00We'll get short on time.
- 52:02Mr. Hypermagnetic out there asked, does SQLAlchemy support paginated results?
- 52:06But that also leads us towards a little area I want to focus on just a couple of things as
- 52:10well.
- 52:11So yeah, maybe address that first.
- 52:13Paginated results is one of those things that seems simple, but it's more of a quagmire
- 52:18than you might expect.
- 52:19There's different ways to do paginated results.
- 52:21The way we all did it years ago was we used these functions that seem called limit and offset.
- 52:25offset where limit will give you like 10 rows at a time and offset will start your result
- 52:29set in the middle.
- 52:30Once we work with bigger database, we all realize that offset is terrible because offset will
- 52:34actually get the whole result set and scroll through it.
- 52:37It does it on the server side, but it's still very slow.
- 52:38So nowadays when you do pagination, you want to have some kind of approach where the query
- 52:42you're doing is looking at some data in the data, some row, the data, some column in
- 52:49the data you're querying.
- 52:50So if you're paginating by date, page 10 would be where date is greater than this date
- 52:55that from the previous page you just got.
- 52:57That's one way.
- 52:58Another way is to write SQL query that do this thing called window functions, where you can
- 53:02figure out all the date or this whatever thing ahead of time.
- 53:05Pagination implies, on that approach, implies that you have, or actually of any approach,
- 53:10implies that you have an order by something.
- 53:13So it's hard to make pagination be like this, like this result here, result.scalars to say
- 53:18.paginate.
- 53:19That's not really, you can make a very crappy version that does that, but it's not really,
- 53:24it wouldn't really work that way.
- 53:25There is, I should say there is a method in the result called partitions that will give
- 53:33you chunks of a result at a time.
- 53:35It's not quite the same as pagination because pagination is stateless, it's usually for web
- 53:39applications where you're going to get a certain page of a result and then show them a web page
- 53:43and then your whole SQL is over.
- 53:44Right.
- 53:45And then you're going to come back to that SQL later.
- 53:46So we have features built in for partitioning of a single result set so that you get chunks
- 53:52of that time, which is helpful.
- 53:53Pagination is something you need to go look, there's some, I think maybe SQL Utils probably
- 53:59has some helper functions for that.
- 54:01We have some recipes in that wiki page that I told you to leave.
- 54:04There's actually some recipes for pagination there.
- 54:07I don't think pagination is like a turnkey.
- 54:09It's got to be customized and people will write pagination frameworks and they're complicated
- 54:14and they're hard to do.
- 54:15So we leave those as an exercise for the community too, but we do have some recipes for that.
- 54:21That's a great answer.
- 54:22Okay.
- 54:23Now for the last thing, I wanted to talk really quickly about this list from Dahlia called
- 54:29Awesome SQLAlchemy.
- 54:30It's one of these awesome lists that covers all these SQLAlchemy things like data structures
- 54:34and types.
- 54:35And I'm just throwing this at you and neither of us have a ton of experience with a lot of
- 54:40these things, but I thought it'd be fun to just kind of go through them.
- 54:42Some of the nice or interesting things that I've ran across here.
- 54:47So it's a way to kind of wrap this up like some extras.
- 54:49All right.
- 54:49So one of them is SQLAlchemy Continuum, a versioning extension for SQLAlchemy.
- 54:56I know what this is.
- 54:57Yeah.
- 54:57Yeah.
- 54:58So it's probably based on one of the recipes that we have in the source distribution.
- 55:04Version A, yeah.
- 55:05It says the first line of SQLAlchemy already has a versioning extension.
- 55:08The extension is very limited.
- 55:09That's correct.
- 55:10Everything he wrote there and everything they wrote there is correct.
- 55:13I have some jobs in the banking industry where I was doing SQLAlchemy and we had some need
- 55:18to version rows.
- 55:19I need to know exactly audit this and when has it changed and how do I go back?
- 55:24All that kind of stuff.
- 55:25Right.
- 55:25Yeah.
- 55:25There's different models.
- 55:26You can either take the rows as you get them and put them in an aircraft table, or you can
- 55:30do this thing where you never update or delete a row.
- 55:33You just insert a new row and you've got a temporal version scheme.
- 55:36I have some recipes to do that in the example section because I did them at a job and I believe
- 55:41Continuum builds upon those.
- 55:42Fantastic.
- 55:43To make a more robust, supported thing.
- 55:45I don't know.
- 55:46I've heard of Liquibase.
- 55:48I don't know.
- 55:49Yeah.
- 55:51I forgot where Liquibase is.
- 55:52I'm not a Java person either.
- 55:54Yeah.
- 55:55But yeah.
- 55:55Okay.
- 55:56Maybe.
- 55:56It might be like that.
- 55:57Here's another one that I ran across from that SQLAlchemy awesome list is SQLAlchemy
- 56:01enum 3.4.
- 56:03I'm guessing because it came from Python 3.4.
- 56:05This package provides SQLAlchemy type to store.
- 56:07I use a standard enum.enums.
- 56:10Because a lot of times what happens is those things get turned into like numbers or other
- 56:14weird things.
- 56:15And here they get a little more type information, right?
- 56:18Yeah.
- 56:18Yeah.
- 56:18This might be obsolete because we do support regular Python enums in our enum type.
- 56:23Yeah.
- 56:24We did it first.
- 56:24This is looking like it's, well, seven months ago.
- 56:27I don't know.
- 56:27If someone's using this, they could stay because there are some enum features.
- 56:31I've been notified that we don't do exactly the way.
- 56:34There's some extra features that I forgot what they were, but there are some things that
- 56:37enums do that we don't support.
- 56:39Maybe it adds that on, yeah.
- 56:40So maybe this probably does that better.
- 56:41Yeah.
- 56:41Enums are really a pain in the butt.
- 56:44Yeah.
- 56:44I know.
- 56:45The data types.
- 56:45I know.
- 56:46All right.
- 56:47The Python side's good, but the database side is.
- 56:49Yeah.
- 56:50Yeah.
- 56:50It's always some sort of hack.
- 56:51Yeah.
- 56:52Alembic.
- 56:53I mean, that's pretty well known to people, but that's obviously important because you've
- 56:57got to keep your database and your models pretty closely in sync, right?
- 57:01Yeah.
- 57:01So we maintain Alembic.
- 57:03I wrote Alembic years ago as SQLAlchemy Migrate was not really holding up to what we were
- 57:09doing.
- 57:09SQLAlchemy Migrate is part of OpenStack now.
- 57:12And I actually have maintained that a little bit as well.
- 57:14But Alembic was meant to be more of a bare bones, a straightforward, not too fancy tool.
- 57:20It has become fancier.
- 57:22And people that use it and get what it's about like it a lot.
- 57:26It's never going to be as cool as South for Django.
- 57:29Maybe an Alembic 3.
- 57:32But that could happen.
- 57:35It's not as automated as everybody would like.
- 57:38It has a system that it will look at your models and look at the database and what we
- 57:43call auto-generate your migrations.
- 57:44But we don't guarantee those migrations are completely perfect.
- 57:48You've got to go and look at them and fix them, which I still think it just did 95% of
- 57:53the typing for you, of the work for you.
- 57:55That's what it was meant to do.
- 57:56Yeah, I think also it's helpful because it works in that realm of that's the area of
- 58:01SQL I know less well, the DDL and that drop.
- 58:04You know, how do I drop a table or a column and then re-add it under a different name without
- 58:09losing data?
- 58:09Like all that kind of stuff can be tricky.
- 58:10Yeah, and a DDL is where a lot of the database vendor specific stuff is exposed.
- 58:15Yeah.
- 58:15Like all these crazy keywords and data types.
- 58:18And it's less declarative and more imperative.
- 58:21And so for Alembic, we just provide a model to create your migrations and it's been working
- 58:26pretty well.
- 58:26It's got a lot of features now, but it's again, a tool where if you don't know what DDL is,
- 58:31you're going to have a bad time with Alembics.
- 58:34Yeah, sure.
- 58:35You should learn what database migrations look like in the alter table, alter column, and
- 58:39know what that means.
- 58:41And then Alembic will be pretty smooth.
- 58:42And there actually are extensions for Alembic.
- 58:44I saw it wasn't on that list.
- 58:45There's an extension called Alembic Utils.
- 58:47Oh, nice.
- 58:47Which are extensions for Alembic for Postgres that also build upon some recipes that are
- 58:52in these docs here.
- 58:52And I recommend looking at that too.
- 58:55All right.
- 58:55There's a couple we can go quick on.
- 58:57There's one for talking to Amazon Redshift as a provider.
- 59:00That's pretty cool.
- 59:01There's Form Alchemy, which...
- 59:04Is this still maintained?
- 59:05This is an old one.
- 59:06Form Alchemy is 12 years old.
- 59:08All right.
- 59:09This is what it is.
- 59:09It's old.
- 59:10Maybe it still works, but let's leave that one alone.
- 59:12It probably still works.
- 59:13Yeah, probably.
- 59:13It looks like it's had...
- 59:14HTML hasn't changed since then.
- 59:16Just style some CSS on it.
- 59:17All right.
- 59:18GeoAlchemy is pretty popular for people doing geospatial work.
- 59:22Yep.
- 59:22So there's GeoAlchemy.
- 59:23There's GeoAlchemy 2.
- 59:24I'm not sure of the relationship.
- 59:26Which one is which?
- 59:27Who works on which one?
- 59:28GeoAlchemy seems like it might have...
- 59:31It might not...
- 59:32I'm not sure how much it's maintained.
- 59:33I think it's maintained.
- 59:33I would like it to be much more well-maintained.
- 59:36I think it supports...
- 59:37It's pretty Postgres-centric, but there are geo things for SQL Server and Oracle and MySQL.
- 59:43I would like to see that stuff supported as well.
- 59:45GeoAlchemy, I don't hear much about.
- 59:47But I think it's a good project.
- 59:49One for some growth, perhaps.
- 59:51SQL tab.
- 59:52SQL profiling and introspection for applications using SQLAlchemy.
- 59:57Wow.
- 59:58Have you seen this?
- 59:58Cool.
- 59:58I have not seen this either.
- 1:00:00What is it doing?
- 1:00:01So when you do a request, I think what it's doing, it stores what the page did.
- 1:00:06So it says here there's 12 queries.
- 1:00:08It spent like 20 milliseconds.
- 1:00:11And then you click on each one and it'll show you the select statements and like the actual SQL.
- 1:00:16So it's probably profiling from the client side.
- 1:00:17Yeah.
- 1:00:18Yeah, yeah, yeah.
- 1:00:19I think so.
- 1:00:19As far as the database.
- 1:00:20Yeah, yeah.
- 1:00:20That's nice.
- 1:00:21If you're just timing it, yeah.
- 1:00:22That's cool.
- 1:00:23I wonder if it does things like explain.
- 1:00:25I don't totally know, but it looks like it has a whiskey integration, which is cool.
- 1:00:29This is another one related sort of that helps.
- 1:00:32It's called N plus one.
- 1:00:34Oh, yeah.
- 1:00:35N plus one is the.
- 1:00:37That's cool.
- 1:00:37Yeah, it's a problem that many ORMs run into when people don't realize they're doing lazy loading and they don't do a join.
- 1:00:44And it can be super indirect.
- 1:00:46Like here I got a query of a list and I sent the list off to the HTML template.
- 1:00:50And the HTML template did a loop and talked about some property on the thing.
- 1:00:54And then there's a bunch of more database queries or something crazy, right?
- 1:00:58Yeah.
- 1:00:58Interesting that it seems to work for multiple lower ends.
- 1:01:01Yeah, SQLAlchemy, PeeWee, and Django.
- 1:01:03So I guess it's streaming and maybe just taps into the APIs of all those different tools.
- 1:01:08Yeah, probably someone's added a layer for each.
- 1:01:09Yeah, I guess you could do it in a kind of a distant way heuristically.
- 1:01:13If you want to see the same query to the same table over and over again, maybe that's what it's looking for.
- 1:01:18Yeah, perhaps.
- 1:01:19Where it could do that without necessarily looking at SQLAlchemy and saying lazy logo called.
- 1:01:23If it just looks at the SQL, there's probably ways to do it.
- 1:01:25But that's an intricate problem.
- 1:01:27But that seems like a really useful tool.
- 1:01:29Yeah, it does.
- 1:01:30And also I saw in there, we'll just close it out with this.
- 1:01:32I saw in there, they mentioned the Pyramid debug toolbar, the Flask debug toolbar, and the Django debug toolbar.
- 1:01:38And I can't speak to Django.
- 1:01:39I don't think I've even run the Flask one.
- 1:01:41But the Pyramid one has a, like, you can open it up and say, what were the SQLAlchemy queries of this page?
- 1:01:47And actually see how many queries.
- 1:01:49And if you're on a page and it says, look, there's 51 queries.
- 1:01:52You're like, I thought I did one.
- 1:01:53What just happened?
- 1:01:53How did I get 51?
- 1:01:54Like, well, you got 50 elements and an N plus one.
- 1:01:58Debugtubo, I haven't really worked with it.
- 1:01:59But yeah, we have a lot of people dealing with it.
- 1:02:02Yeah, yeah.
- 1:02:02We have to fix issues with it.
- 1:02:04I don't actually write any web applications anymore.
- 1:02:06I just do this.
- 1:02:07So I don't get to do that.
- 1:02:08I don't get to see that stuff.
- 1:02:09Oh, really cool.
- 1:02:10I also don't use them very much anymore either.
- 1:02:12I find that I don't need that support as much as I did in the early days.
- 1:02:15But I do remember them being quite valuable early on.
- 1:02:17All right, Mike.
- 1:02:19Well, there's more stuff we could go into.
- 1:02:20But what a cool conversation.
- 1:02:23And thank you so much for releasing the 2.0 stuff, for adding the async support.
- 1:02:27It really opens up a lot more use cases.
- 1:02:29I think that are going to be interesting for people.
- 1:02:31So, yeah, it's fantastic.
- 1:02:33Yeah, my pleasure.
- 1:02:33The async worked out really well.
- 1:02:35I'm really happy about that.
- 1:02:36Now, before you get out of here, let me ask you the final two questions.
- 1:02:38You kind of alluded to this already.
- 1:02:40If you're going to write some code, what Python editor do you use?
- 1:02:43I'm on VS Code right now.
- 1:02:45I was on Sublime for a long time.
- 1:02:47Years ago, I used TextMate.
- 1:02:49I do use VI and Vim a lot.
- 1:02:52But if I have lots of windows open, right now it's VS Code.
- 1:02:55Fantastic.
- 1:02:56And then notable IPI package.
- 1:02:59Anything come to mind?
- 1:03:00I mean, we kind of threw out a whole bunch.
- 1:03:01Probably not.
- 1:03:02Did I put anything?
- 1:03:03I'm going to click to that.
- 1:03:04I didn't put anything down.
- 1:03:04How about your favorite out of the list that I threw up there?
- 1:03:08Tell me, like, out of the awesome ones, which one stood out the most to you?
- 1:03:12Oh, that N plus one thing looks really interesting.
- 1:03:13All right.
- 1:03:13Awesome.
- 1:03:14So N plus one with the plus spelled out.
- 1:03:16And the one also spelled out.
- 1:03:17Yeah.
- 1:03:18That's cool.
- 1:03:18Yeah.
- 1:03:19Yeah.
- 1:03:19Very good.
- 1:03:20All right.
- 1:03:20Well, final call to action.
- 1:03:21People have a bunch of SQLAlchemy code that they've written, but it's probably for the
- 1:03:25older style.
- 1:03:26It's probably not async and so on.
- 1:03:27What do you tell them?
- 1:03:28Yeah.
- 1:03:29We have a brand new tutorial on the current website.
- 1:03:32If you go to just docs.seqlalchemy.org.
- 1:03:34On the left side, it'll have this 1.4 slash 2.0 tutorial that kind of represents SQLAlchemy
- 1:03:40all over again using all the newest concepts.
- 1:03:44I would look at that and just get to know it and also point out problems.
- 1:03:48Yeah.
- 1:03:49This is a new tutorial.
- 1:03:50So this supersedes the old tutorials.
- 1:03:52And it's going to talk about core and ORM at the same time.
- 1:03:55This is a complete brand new rewrite from the ground up.
- 1:03:58It took many weeks to do it.
- 1:03:59And then there's also the migration guide.
- 1:04:01But if you go through this tutorial, you'll really see what the new way of working is supposed
- 1:04:05to look like and what the idea is what's supposed to be.
- 1:04:08If you look at the, if you read the tutorial and assume you don't have any SQLAlchemy code,
- 1:04:12assume you're just learning it from scratch, see what it's like.
- 1:04:14See, wow, wow, this is different.
- 1:04:15Or you might see, oh, I never knew that was like that.
- 1:04:18Because it really tries to represent the library from the first principles, so to speak,
- 1:04:24up to ORM stuff.
- 1:04:26All right.
- 1:04:27Well, I'm excited about all these new features.
- 1:04:29Even the embracing of context managers everywhere.
- 1:04:32It looks great to me.
- 1:04:33So yeah, thanks for being here.
- 1:04:35And thanks for sharing this with everyone.
- 1:04:36Yeah, my pleasure.
- 1:04:37Bye.
- 1:04:37This has been another episode of Talk Python To Me.
- 1:04:41Thank you to our sponsors.
- 1:04:43Be sure to check out what they're offering.
- 1:04:45It really helps support the show.
- 1:04:47With TopTal, you get quality talent without the whole hiring process.
- 1:04:51Start 80% closer to success by working with TopTal.
- 1:04:55Just visit talkpython.fm/toptal to get started.
- 1:04:59Do you need a great automatic speech-to-text API?
- 1:05:02Get human-level accuracy in just a few lines of code.
- 1:05:05Visit talkpython.fm/assemblyai.
- 1:05:07Want to level up your Python?
- 1:05:09We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:05:13Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:05:19And best of all, there's not a subscription in sight.
- 1:05:21Check it out for yourself at training.talkpython.fm.
- 1:05:24Be sure to subscribe to the show.
- 1:05:26Open your favorite podcast app and search for Python.
- 1:05:29We should be right at the top.
- 1:05:31You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the
- 1:05:36direct RSS feed at /rss on talkpython.fm.
- 1:05:39We're live streaming most of our recordings these days.
- 1:05:43If you want to be part of the show and have your comments featured on the air, be sure to
- 1:05:47subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:05:51This is your host, Michael Kennedy.
- 1:05:53Thanks so much for listening.
- 1:05:54I really appreciate it.
- 1:05:55Now get out there and write some Python code.
- 1:05:57Bye.
- 1:05:58Bye.
- 1:05:59Bye.
- 1:06:00Bye.
- 1:06:01Bye.
- 1:06:02Bye.
- 1:06:03Bye.
- 1:06:04Bye.
- 1:06:05Bye.
- 1:06:06Bye.
- 1:06:07Bye.
- 1:06:08Bye.
- 1:06:09Bye.
- 1:06:10Bye.
- 1:06:11Bye.
- 1:06:12Bye.
- 1:06:13Bye.
- 1:06:14you
- 1:06:14you
- 1:06:15Thank you.
- 1:06:17Thank you.