Latest / Talk Python To Me / #5: SQLAlchemy and data access in Python
Transcript
- 0:00Talk Python To Me, episode number five, with guest Mike Baer, recorded Thursday, April 9th, 2015.
- 0:07Hello and welcome to Talk Python To Me, a weekly podcast on Python,
- 0:37the language, the libraries, the ecosystem, and the personalities.
- 0:40This is your host, Michael Kennedy.
- 0:41Follow me on Twitter, where I'm @mkennedy,
- 0:45and keep up with the show and listen to past episodes at talkpythontome.com.
- 0:49This episode, we'll be talking with Mike Baer about SQLAlchemy.
- 0:53Let me introduce Mike.
- 0:54Mike Baer is the creator of several prominent Python libraries, including SQLAlchemy,
- 1:00Mako Templates for Python, Alembic Migrations, and Dogpile Caching.
- 1:04He's been working with open source software and databases since the mid-1990s.
- 1:09Today, he's active in the Python community, working to spread good software practices to an ever-wider audience.
- 1:14Mike is a semi-regular presenter at PyCon US,
- 1:17and has also spoken at many smaller events and conferences in the United States and Europe.
- 1:22Follow Mike on Twitter, where he's Zeke.
- 1:24That's at Z-Z-Z-E-E-K.
- 1:27Welcome to the show.
- 1:28Thanks. Thanks for having me.
- 1:30Yeah, I've been a longtime fan of SQLAlchemy.
- 1:33And, you know, when I started this podcast, I was thinking,
- 1:37who do I have to have on the show?
- 1:38And you were definitely on the list.
- 1:39That's very flattering.
- 1:41Yeah, you built some great stuff.
- 1:44So, before we get into the details of SQLAlchemy and how we should use it and why it's so awesome,
- 1:49maybe let's take a trip down memory lane, step back a little bit,
- 1:53and just, you know, how and when did you get into Python?
- 1:55I got into Python after kind of hesitating to get into it, meaning I knew about it,
- 2:00and it seemed like something that was appealing to me.
- 2:03But I kind of resisted mostly because of the white space thing.
- 2:06This is back in probably 2003.
- 2:09I was working at Major League Baseball.
- 2:11Most of my programming career before Python was doing a lot of Java and a lot of Perl.
- 2:18And I really liked the idea of object-oriented languages,
- 2:23and I liked the idea of scripting languages.
- 2:25So Java was very object-oriented.
- 2:28Perl was not too good at objects.
- 2:30But it was a scripting language.
- 2:32So I really wanted there to be some scripting language where I could write much cleaner code than you get in Perl.
- 2:37Yeah, so you kind of had half of what you wanted in one language.
- 2:40Yeah.
- 2:41But you wanted to bring it all together, right?
- 2:43And Python really looked like it.
- 2:45Python really looks like it, but it's got that stupid white space thing.
- 2:48And I just didn't want to get into it.
- 2:50And then one day when I was working at baseball,
- 2:53we were rolling out this client application called WinCVS, I think it was.
- 2:58It was a Windows CVS client.
- 3:00And we had to roll it out to people that were not programmers.
- 3:02And we needed to add hooks to it so that people could tag things and send things to different servers by tagging.
- 3:10And WinCVS's scripting language was Python.
- 3:13So I had to learn, you know, 10 lines of Python to do this script.
- 3:18And basically I spent like two hours with the white space thing.
- 3:21And I was like, oh, we do that white space anyway when the code is clean.
- 3:26And I'm actually totally fine with it.
- 3:27So let me just learn Python now that I've spent 10 minutes getting used to white space.
- 3:32That's really funny that it was like a real barrier to you, right?
- 3:36And then it just came down.
- 3:37It really bugged me because I was a really sloppy coder back then.
- 3:40Python really made me a much cleaner programmer.
- 3:43Yeah, that's a good point.
- 3:45I hadn't really thought about that.
- 3:46But it definitely does make you think about that.
- 3:49And, you know, a lot of the modern editors make it almost transparent to you.
- 3:52But I think there's the mental concept of it.
- 3:55And then there's the reality of it.
- 3:56And the reality is that it's actually not a big deal at all.
- 3:58It's super smooth.
- 3:59But when you're coming from, say, Java or C# or C++ or something, then it seems like a huge deal.
- 4:05Yeah, you get over it really quickly.
- 4:08Excellent.
- 4:09Oh, you bring up WinCVS.
- 4:10Boy, I used to use that thing back in the late 1990s or something.
- 4:15That brings back some memories.
- 4:16Yeah, that's kind of what it was, yeah.
- 4:17That's awesome.
- 4:18Let's talk about something more modern than WinCVS.
- 4:21Let's talk about SQLAlchemy.
- 4:23Sure.
- 4:23So can you just tell, I suspect that, you know, 90% of the listeners are fans of and familiar with SQLAlchemy.
- 4:31But there's going to be some folks who don't know or maybe they know it as a buzzword.
- 4:34Can you just tell everyone what SQLAlchemy is?
- 4:37So SQLAlchemy is this library that is, it's all about Python.
- 4:41And it's all about SQL and databases and interacting with databases.
- 4:47So it's basically when you need to work with a relational database, say you're going to work with like Postgres or MySQL, there's all kinds of things you need to do with the database.
- 4:56You need to write, you know, programs that talk to it.
- 4:58You need to get results back.
- 4:59You need to create schemas.
- 5:01You might need to be given some existing database and look at it.
- 5:05There's scripting and migrations of schemas you want to do.
- 5:08You probably are writing applications that have higher level business concepts that you want to map to that database.
- 5:15So SQLAlchemy started out with a really ambitious goal of having a place, a way to do that, a way to do all those things in Python.
- 5:24Not just being able to map like an object to a table, but all the features of managing schemas and creating schemas and reflecting them and working with SQL scripts and everything else.
- 5:36Working with data type problems, making all kinds of different database backends look as similar as is feasible.
- 5:44You know, like data types in Oracle are very weird compared to those in MySQL and SQLite's a whole different story.
- 5:51Those databases are always going to be different, but you can at least try to get some semblance of sanity across all of them.
- 5:56So that's why if you go to SQLAlchemy's website, the first thing it says is like the database toolkit for Python.
- 6:01It doesn't say it's an ORM.
- 6:02That's just one component.
- 6:04It's a toolkit that has kind of helpers for whatever you have to do with a database.
- 6:11SQLAlchemy can definitely have a role.
- 6:13That's excellent.
- 6:13You have at least two really nice sort of comprehensive walkthroughs, one for the core and one for the ORM component.
- 6:20That's SQLAlchemy.org, right?
- 6:23Yes, SQLAlchemy.org.
- 6:25So when did you get started with, when did you create SQLAlchemy and what inspired you to create it in the first place?
- 6:31So I was kind of writing the first iteration of SQLAlchemy in late 2005, and our first release was early 2006.
- 6:39And really when I was writing SQLAlchemy, it was not the first time I was creating a tool like that.
- 6:45When you work, or at least in the old days, when you used to work in the 90s in all these various internet shops, we had really little tools to work with.
- 6:55I mean, there were a lot of tools, but we were, you know, using Perl and libraries to talk databases were pretty crude.
- 7:01And I was using Java from day one when it was, you know, version 1.0, and there was literally no libraries before Hibernate.
- 7:08So throughout the 90s, you know, you get into the habit of writing little database access layers yourself.
- 7:15And every job you have, you're going to go and, now I'm going to write a really better database.
- 7:21You know, the database access layer I wrote last time sucked.
- 7:23Now I'm going to write a really good one.
- 7:25And you say that statement to yourself like five times, right?
- 7:29Yeah, every job you have, it's like, okay, this time we're going to get it right.
- 7:32We're going to write the ultimate, you know, set of tools, you know.
- 7:36And then as you're doing this, you know, as the 2000s rolled along, ORMs, you know, Hibernate suddenly was around.
- 7:42And I was doing a lot of Perl at MLB, and there were some ORM-ish things with Perl that looked terrible to me.
- 7:48They looked really simplistic.
- 7:51I had worked, by the time I was at MLB, I was working, that was, they were a big Oracle shop.
- 7:55I had already had a lot of gigs where I had worked a lot with Oracle.
- 7:58I had worked a lot with Postgres.
- 8:00I had worked a lot with Sybase, Microsoft SQL Server.
- 8:03I had worked a lot with not just MySQL, but its earlier MSQL incarnations in the early 90s.
- 8:10So I had, and I had written database code in Perl, Java, and C, C++, you know, because back in the early 90s, you know, we didn't, Perl wasn't always available for some cases.
- 8:23So I really had iterated a lot on this whole back and forth with the database, sending a SQL string, getting a result back, getting data back from the result.
- 8:30I knew a lot about that.
- 8:33So, and I always wanted to have a system that would be the last time I have to do this, you know.
- 8:40Yeah, I had that feeling.
- 8:41A long time, it was like, I was writing things in Java.
- 8:43I was going to write like a big toolkit in Java, like the big, you know, web framework, everything you need Java thing.
- 8:49And it never really happened.
- 8:50But eventually when I got into Python, that's when I really began doing it.
- 8:54I, you know, Python had kind of not a lot of libraries around.
- 8:58The only library that was feasible for database access on Python was SQL Object, which is a great library and was a huge influence on SQLAlchemy.
- 9:08And I started doing it and it was kind of based on the latest techniques I iterated at baseball.
- 9:14Like the idea is that if you want to talk to a database table, you make a data structure that represents what the table looks like.
- 9:20And we call that table metadata.
- 9:22But one critical thing I did with SQLAlchemy was I actually decided to read a book beforehand.
- 9:28So I had this book called Patterns of Enterprise Architecture by Martin Fowler.
- 9:32Yeah, that's a great book.
- 9:33And I read that book and half of the book was like, oh, that, oh, this, oh, this.
- 9:38All these things that I've done.
- 9:39And he kind of put names to them.
- 9:40And then as he did all that, there were some other patterns that I was not familiar with, like the unit of work pattern.
- 9:46I was like, wow, look at that.
- 9:46I've never heard of that.
- 9:47That's pretty cool.
- 9:48And does that appear as the session in SQLAlchemy now?
- 9:51Yeah, the session.
- 9:52Yeah.
- 9:52And actually session is from Hibernate.
- 9:54Hibernate's got the same thing.
- 9:55So I, you know, really made a go at it.
- 9:58And I said, I'm actually going to read a book beforehand and really try to get all these patterns right.
- 10:02And that's kind of how it started.
- 10:05And it took a very long time for SQLAlchemy to be any good.
- 10:09I mean, I think when I first released it, it had a lot of the special things going on that made people like it.
- 10:15But it was not executed very well.
- 10:17But, you know, over the years, over really 10 years, it's gotten very refined.
- 10:22But it's taken a long time.
- 10:23Yeah, it's quite a mature product.
- 10:24And that's cool.
- 10:25Yeah.
- 10:26Did you use it internally before you actually released it?
- 10:29Or was it kind of, I'm building it out in public?
- 10:31Yeah, no, it was funny.
- 10:33When I was starting in Python, I was writing open source.
- 10:35The first thing I wrote was this template language called Mighty, which was basically almost a line for line port of a template thing in Perl called HTML Mason.
- 10:42That was the first open source thing I published.
- 10:45And then Mighty was kind of an embarrassment eventually.
- 10:48And I wrote Mako to replace it.
- 10:50But I was writing these libraries at first without having any job.
- 10:55The gigs I was doing were still Java jobs.
- 10:57I was still doing, after I left baseball, I was still doing Java work.
- 11:00Python was certainly used in the mid-2000s, but it was not as dominant as it is today.
- 11:06I didn't actually get to use SQLAlchemy for a real gig until version 0.5.
- 11:11So maybe four years into it, I finally was on a gig where like, hey, we can do this thing in Python and we can do this.
- 11:17And everyone was like, okay, use the Python thing.
- 11:20Yeah, use that toy language.
- 11:22Yeah, yeah, yeah.
- 11:22It was kind of like that, yeah.
- 11:24And it was actually the website for a TV show called Charlie Rose, which is on PBS.
- 11:28Oh, yeah, yeah.
- 11:28Great.
- 11:29Yeah, we wrote, I don't know if it's still up in that incarnation anymore, but at the time, this is maybe 2007, it was written using early version of pylons.
- 11:38And that was the first time I used SQLAlchemy on the job myself.
- 11:40And SQLAlchemy improved by leaps and bounds on that gig because I had like, oh, this sucks.
- 11:45Oh, look at this thing.
- 11:46This thing is terrible.
- 11:47How could people have been using this for three years?
- 11:49Yeah, that's excellent.
- 11:50Yeah.
- 11:51So it's weird.
- 11:52Yeah.
- 11:53It was written kind of in a vacuum.
- 11:55Like I only knew what my users told me for several years.
- 11:58Yeah, very cool.
- 11:59The previous show that's not out yet, but was recorded just before this is a guy named Mahmoud Hashemi from eBay PayPal.
- 12:06And the whole show is about sort of enterprise Python and sort of using it as a real proper language.
- 12:12And I think, you know, it's probably that time frame and that era.
- 12:15There's a lot of similar ideas about, hey, we could actually build real websites and, you know, web scale professional apps with this language.
- 12:23It's great.
- 12:24Yeah.
- 12:24Yeah.
- 12:25I mean, Python as enterprise was something that I think, you know, it existed in the early 2000s.
- 12:33Like I think a lot of people were using, I mean, people were using like Zope or Plone, I think was the early, you know, incarnation of enterprise Python.
- 12:40But I think, you know, really you got to hand it to like, I mean, first Ruby on Rails got people more comfortable with scripting languages for enterprise.
- 12:48And then Django really helped a lot too.
- 12:51Django really brought a lot of people to Python.
- 12:54And I think the critical mass kind of started happening for Python kind of in 2005 on forward.
- 13:02Absolutely.
- 13:03And I think, you know, to shoot your horn and everybody else's a little bit, the stuff that's in PyPy, all those packages out there make it really hard to not consider Python.
- 13:14The fact that you can just pip install magic, you know, and build stuff so quickly.
- 13:19Like, why would I start from scratch, you know?
- 13:21Right.
- 13:21Absolutely.
- 13:22We now know what SQLAlchemy is, if we didn't.
- 13:24Can you kind of give us like a, I know it's hard to talk about code, so don't get too detailed.
- 13:28But can you give me a little walkthrough of like what is involved in getting started?
- 13:32Like what do I do in code to maybe make a basic query or connect to the database?
- 13:37So there's different ways that you might want to use the system.
- 13:40I mean, there's the level, the layer of I want to just make a query.
- 13:43And that's pretty easy.
- 13:45I mean, SQLAlchemy has a lot of different levels that it can be used at.
- 13:49Like if you want to just connect to a database and make a query, that's like a really two line.
- 13:52You make an object called an engine, which you give it a URL for a database.
- 13:57And then the engine can just accept queries as strings directly.
- 14:01And right there, you've already saved maybe 10 lines of code versus what it would be with the raw Python database.
- 14:08Yeah, that's fantastic.
- 14:09Maybe we should take a step back and talk about the layers.
- 14:11And then we could talk about, you know, well, this layer, that layer, it looks like such and such.
- 14:15So you have the core and you have the ORM.
- 14:17Can you talk about that a little?
- 14:19Yeah, so I actually wrote SQLAlchemy in this inside-out way where I didn't even worry about the –
- 14:25I mean, I kind of knew what I wanted to do for the ORM, but I didn't even worry about that for a few months.
- 14:28I wrote first just the engine, just like the concept of here's how to connect to a database using the Python DB API.
- 14:34And here's a way that we can execute a query and get results back in a way that's a little bit nicer than what the raw Python database API gives you.
- 14:44It's a little bit nicer and a little more consistent.
- 14:47Then the next thing I did, which, you know, was soon after, was I wrote the table metadata system, which I had already done many times at different jobs.
- 14:54You know, kind of looked in Fowler's book.
- 14:56So you wrote – you create this concept of a Python data structure that mirrors what structures you have in your database.
- 15:06So when you have a relational database, it kind of – without using it at all, it has a fixed schema.
- 15:12You have, like, tables that are kind of – they're not – we might call them physical tables, which they're really not physical.
- 15:17They're just on a disk.
- 15:18But they're tables, and the tables have columns, and they're kind of like the things that are going to store your data.
- 15:24So you model a mirror of all that stuff in Python so that you can write Python code that refers to this object structure that can then relate directly to how the database is built.
- 15:36So that's called database metadata.
- 15:38So it means it's information about the structure of your database.
- 15:42Then from that, the table metadata objects in SQLAlchemy have all these methods on them, like select and update and, you know, table.select.where.
- 15:52And when I say, you know, x.y.z, the dot in Python means that we're calling a method on an object.
- 15:58And if you have this pattern where you say an object.call this, and then you get something back, and then you say .call that,
- 16:05that's a pattern called method chaining, which means that you keep calling methods on this object that keeps returning a new copy of itself.
- 16:13So that's a common pattern used in database query libraries because the method chaining is kind of like you're building up this data structure
- 16:20that looks like basically a syntax tree.
- 16:23A syntax tree basically represents like some code, but it represents the code as a set of objects connected together in a tree.
- 16:31Yeah, that's fantastic.
- 16:32I really like the fluent API that you put together there where basically you start with a query, and whatever you do to it,
- 16:37what comes out of the query and just lets you build up.
- 16:40Here's a where clause.
- 16:41Here's an order by.
- 16:42Here's a distinct or whatever.
- 16:44Yeah.
- 16:44At this point, I mean, that pattern is pretty commonplace these days.
- 16:48I mean, when I first did it, it was not quite as commonplace.
- 16:50I mean, basically Java Hibernate kind of has that pattern, but not in a nice way because Java is just not as slick of a language as Python.
- 16:58And some of the, you know, it was a pattern that was around.
- 17:01I think SQL object had it as well, you know.
- 17:03It builds on that, but then we also did this thing.
- 17:07I took this thing right from Ian Bicking's SQL object, which was that you overload Python operators like the double equals operator or the greater than operator,
- 17:16so that when you have these little objects that refer to columns and values in a database, you can compare them with the Python comparison operators,
- 17:25and they kind of auto-magically create another object like a comparison.
- 17:30And if you go through the ORM or the core tutorial on SQL object, the core tutorial would really show how this works at the expression level.
- 17:41The ORM tutorial kind of refers to it, but it's more about how to get the ORM going.
- 17:46But what I'm talking about here is how the core query system works.
- 17:48So basically these Python objects can kind of expressly be combined to create SQL queries.
- 17:56And that's the core, really.
- 17:57That's really what you get, as well as the fact that it returns objects, it returns Python objects.
- 18:02Like if you query for dates, you'll get a Python date object back.
- 18:06If you query for intervals, you can get a Python time delta back.
- 18:09And there's all systems for building your own custom types if you want to get JSON back, things like that.
- 18:15And then the ORM builds on top of that.
- 18:17So if you're starting out, I mean, if you really want to learn the library from the ground up, I would start with the core and then move to the ORM.
- 18:22If you want to get some quick results for a program you're writing, you might want to start with the ORM first and then do a deeper dive later.
- 18:29I've kind of switched those directions around over the years, and I think there's just two ways to learn.
- 18:34You can learn from the outside in or learn from the inside out.
- 18:37And it's kind of your choice as to do you want immediate gratification or do you want more fundamental knowledge first?
- 18:45Right.
- 18:45Well, and how much are you going to be depending on the library?
- 18:48Right.
- 18:50If it's your core business app and it's really important that this thing keeps working, maybe you should deeply understand it.
- 18:55If it's just a little blog you're throwing together, do it quick and then come back and learn it more deeply.
- 18:59Right.
- 18:59Start with the ORM and then go to the core to understand that.
- 19:02Yeah.
- 19:02Well, everyone I've seen, everyone's got a different way to do it.
- 19:05So I just try to present both.
- 19:07And also for a really long time, the core didn't have that name core, and that was a problem.
- 19:11It was just called SQLAlchemy, and then there was SQLAlchemy ORM, and people didn't really, you know, obviously understand.
- 19:17There's this whole thing with SQLAlchemy that is not the ORM.
- 19:20So I introduced the word core probably about, you know, five years ago to say, hey, there's this whole other thing.
- 19:27And I divided the docs into like two big left and right sections so that you can see there's this whole other thing.
- 19:33There's only the core, and it's like an entire set, the right half of the docs.
- 19:37Yeah.
- 19:38I think you have them almost as columns next to each other.
- 19:40I do.
- 19:40I kind of imitated.
- 19:41I think Django's documentation at the time had that thing going on with the two columns.
- 19:45So I did that to say like, look, this is whole core thing.
- 19:48If you don't like the ORM, because I knew the ORM was going to be, the ORM is more opinionated.
- 19:52I knew it would be controversial.
- 19:54I knew that I personally didn't like ORMs very much.
- 19:57And this was going to be the ORM that I was actually going to like.
- 19:59But I knew all the other commercials like me were going to hate it.
- 20:02And those people still around today who hate ORMs.
- 20:04So I wanted to say like, look, I get it.
- 20:06Don't use the ORM.
- 20:07Use the core part.
- 20:08And then when you kind of use it for a long time, you realize, hey, this redundant thing
- 20:12I'm doing over and over again could just kind of be automated by the ORM.
- 20:14Then you can use the ORM when you see there's a need for it.
- 20:17So that's kind of how I saw it being used.
- 20:19But other people go the other way.
- 20:21Sure.
- 20:21So you've sort of talked about the learning perspective of when I might use the core versus
- 20:26the ORM.
- 20:26But are there like performance or other patterns?
- 20:29Like what other considerations do I choose between the core and the ORM model?
- 20:33There's a lot of performance.
- 20:35There's a lot of, yeah.
- 20:36So kind of the unspoken drama of SQLogamy for all the years it's been out has been the performance
- 20:42drama.
- 20:42Because coming from a Java background, you know, in the 90s, early 2000s, at least back then,
- 20:50when we did Java programming, we really didn't pay too much attention to the way our code
- 20:55was written might be slower or faster.
- 20:57I mean, obviously, you know, if you're doing like order of N, if you want to do something
- 21:00once rather than N times, things like that.
- 21:02But just if you do two nested loops or you do this special library call instead of the
- 21:07loop, is that faster or slower?
- 21:09You don't see that much in Java because all the standard libraries in Java are written in
- 21:13Java as well.
- 21:13But with Python, it's mostly CPython.
- 21:19There's this big gap where if you do something in a for loop, it might be very slow.
- 21:24And you do the exact same thing with like the zip call.
- 21:28It's a million times faster because the zip call is written in C.
- 21:31So there's this whole thing with Python where you have to kind of use the standard library
- 21:37as much as you possibly can because you want as much data processing to happen in native C
- 21:42functions rather than in Python.
- 21:45So I didn't know that in the first few years of writing SQLAlchemy.
- 21:48And I didn't really know much at all about what was fast and what was slow.
- 21:50And that's pretty common when someone starts with Python.
- 21:54So up and through to version three, the thing performed horribly.
- 21:58And then there was a lot of blog posts and other competitors that came out to try to challenge
- 22:03the performance of SQLAlchemy.
- 22:04And I went on kind of a multi-year mission to improve the performance of the system.
- 22:09So these days, and the most recent incarnation of that has been more performance improvements
- 22:14in version 1.0, but also a lot of new documentation that I've added in version 1.0 that refers specifically
- 22:20to the notion of performance.
- 22:22It's actually in our FAQ now.
- 22:23There's a whole section of my performance.
- 22:24There's entire sections of how to profile an application.
- 22:28It's, and there's a lot of sample tests, example suites in version 1.0 where you can compare
- 22:35different methods of doing the same thing for their performance slash ease of use tradeoff.
- 22:42So I have examples of, you know, if you want to do this query, how fast is this query using
- 22:47the core by itself, how fast is this query using the ORM, but loading individual columns,
- 22:52how fast is this query if we use the ORM full blown, and what are the tradeoffs, and how
- 22:57can we switch between those two systems?
- 22:59So that's really interesting.
- 23:01I have a couple of performance questions I'd like to ask about.
- 23:04First, you said you went on this multi-year mission to speed it up, which is, that's a lot
- 23:09of dedication, and that's awesome.
- 23:10What kind of performance gains did you get?
- 23:13Did you like double the speed, or what happened?
- 23:15Over the course of, since version 3, I'm sure we've quadrupled the speed of it, because
- 23:20it was really, really slow in version 2 and 3.
- 23:23I mean, there's things, there's all kinds of Python idioms that change performance so
- 23:27dramatically, and it's kind of a way of life for me now, but if you don't know, if you come
- 23:32from like C#, it's probably going to be really painful to learn.
- 23:36Like, if you do things like, if you want to check if a key is in a dictionary and add
- 23:40a value, if you do that check with using, if you basically try to get the key, and then
- 23:46it raises a key error, and you catch the key error, and you say, okay, let's put the value
- 23:49in now, that's going to be way, way, way, like five times slower than doing it by just doing
- 23:54a if key in dict first.
- 23:56Right, absolutely.
- 23:57Assuming the dictionary is usually not going to have the value.
- 24:02Yeah, because it's actually the throwing the exception that is the raise, yeah, it's super
- 24:06expensive.
- 24:06Yeah, crazy.
- 24:07So that's one.
- 24:08Then there were things like, if you have an object, and you want, there's this pattern called
- 24:13the proxy pattern, it's a programming pattern, where if you have an object, it has a bunch
- 24:16of attributes, and as you call those attributes, it's actually sending the message to some other
- 24:20object that's kind of embedded into it, it's called a proxy, and that's a very common pattern.
- 24:24If you'd use the, there's a really easy way to do the proxy pattern in Python, which is
- 24:28use this magic method called get at her, it's double under get at her.
- 24:31And you get at her is past the name of the attribute trying to get, and then you say,
- 24:35oh, let's call that attribute from our little nested object.
- 24:38Crazy slow if you do it that way, compared to if you actually put explicit descriptors on
- 24:44the proxy class that go directly to the inner proxy class.
- 24:48So very early SQL alchemies, I use a lot of get at her, and people said, hey, this get
- 24:53at her is crazy slow.
- 24:53This thing is using get at her everywhere, and it's super slow, because they're all function
- 24:57calls, and function calls are very expensive in Python.
- 24:59So, got rid of all the get at her.
- 25:01You do this procedure in programming, you do a lot with Python, which is called inlining.
- 25:07Inlining means if you have some operations where method A calls method B, calls method C, calls
- 25:13method D, you inline them.
- 25:15You take all the code and you unwrap it into one big function, so that there's no function
- 25:20calls within it.
- 25:22Inlining is something that you kind of get someone for free if you use a newer interpreter
- 25:28like the PyPy interpreter, which has a just-in-time compiler that does a lot of inlining for you.
- 25:33That's really interesting.
- 25:33I was going to ask you how SQLAlchemy works with PyPy, and have you tried Pysten from the
- 25:38Dropbox project?
- 25:41I haven't used Pysten yet.
- 25:42Pysten looks like, I mean, I haven't looked at it much.
- 25:46It looks like kind of the next incarnation of a thing called Unladen Swallow, which, if you
- 25:51remember, that was, I think, a Google project where they were trying to use the LLVM compiler,
- 25:56and I think that's what they're doing with the Pysten thing.
- 25:59Oh, interesting.
- 26:00The same one that Apple uses, yeah.
- 26:02Yeah, I don't know what I'm talking about, but there's a compiler just-in-time platform
- 26:08called the LLVM, and I don't know much about it.
- 26:09Yeah, that was Chris Lattner's project, and he's the guy who created Swift, and I think
- 26:14they're using it there for Swift as well, so interesting.
- 26:17Yeah, I haven't used Pysten.
- 26:19Yeah, I mean, I see some news about it.
- 26:22I know Guido works at Dropbox now, so I guess we'll see.
- 26:25Yeah, that should give Pysten a little more credibility than maybe just a random project,
- 26:31right?
- 26:31Yeah.
- 26:32Michael here.
- 26:52Thank you so much for listening to and spreading the word about Talk Python TV.
- 26:57The response to the podcast continues to be wonderful and humbling.
- 27:01I have a quick comment about supporting and sponsoring the show.
- 27:03I'm still looking to line up stable corporate sponsorships, but I wanted to tell you about
- 27:07a community-based campaign I'm launching to allow listeners to directly support the show.
- 27:11We are running a Patreon campaign.
- 27:14You might not have heard about Patreon, but it's kind of like Kickstarter for things like
- 27:17podcasts, which release frequent small deliverables rather than one-off large engineering projects.
- 27:23Visit patreon.com/mkennedy.com/mkennedy and watch the video to see how you can donate as little
- 27:31as $1 per episode to support Talk Python To Me.
- 27:34This is your chance to ensure that the Python community continues to have a strong public voice.
- 27:41Consider supporting us today at patreon.com/mkennedy and thanks for listening.
- 27:45It works with pypy though pretty well, Sequel Alchemy.
- 27:55Yeah, it took a long time for pypy because the pypy itself had a lot of quirks years ago.
- 28:12And yeah, pypy is on our continuous integration system.
- 28:18pypy doesn't use the same kind of garbage collection that CPython does.
- 28:22And garbage collection is kind of a big deal when you write a database library because it affects
- 28:30when you have reference cycles getting garbage collected.
- 28:34A lot of our tests would fail because they were relying upon things being garbage collected automatically.
- 28:39And that doesn't really necessarily happen as much with pypy.
- 28:42So basically, if you have a test that doesn't tear itself down correctly and you still have a connection open
- 28:47that access a table and you run Postgres, the teardown of your test will refuse to drop a table
- 28:54because Postgres still has a lock on it.
- 28:55So there's a lot of issues like that that pypy keeps us very honest on.
- 28:59So that's good.
- 29:00pypy seems to use more memory, but it runs definitely much faster.
- 29:06And I think it's a great interpreter and I'm really hopeful for pypy.
- 29:09Yeah, that's really interesting.
- 29:10So we're into it.
- 29:11Yeah, for sure.
- 29:12Cool.
- 29:13So one thing that I think is nice about the ORM version or the ORM layer is I can get these classes back from my database
- 29:23and make changes to them and just basically call commit on the session.
- 29:27So can you talk about the change tracking?
- 29:29So that's the unit of work.
- 29:31So this is the thing.
- 29:32When I first read SQLogamy, that was the part of this whole thing that I was not even familiar with myself
- 29:36because I read Fowler's book and he's like, you know, enterprise software uses this unit of work thing
- 29:41where you just kind of accumulate the changes and then you just push them all in one shot.
- 29:44And I was like, wow, that's crazy.
- 29:47And, you know, I could see how it worked.
- 29:49I'm like, that's really, you know, pretty easy.
- 29:50You just track it so that all your inserts and your deletes and your updates get kind of put into a big bucket and you sort them out.
- 29:58And I wrote a very simplistic version of that for SQLogamy 0.1.
- 30:02It was really bad because I really didn't quite know how to write it.
- 30:06I was like, I mean, I knew how to write it, but I did this kind of coding, which I did more back then probably that I maybe would term seat of your pants coding,
- 30:15where you kind of know that this part connected that part and this part connected that part.
- 30:19But my brain couldn't see the whole thing at once.
- 30:23And it produced, if you look, you can get the code, go look at old 0.2 of the unit of work and you will be like, what is that?
- 30:30It's crazy.
- 30:31But when I first saw it work, I was like, that's amazing.
- 30:37Yeah, it's super nice.
- 30:38It's really easy.
- 30:39Because I was logging to SQL.
- 30:40Yeah.
- 30:40And I was like, that's, I think I said, this, that's when it got really, got me really jazzed.
- 30:44Because like, this thing's going to be big.
- 30:46When I saw it do that commit the first time and I saw it, all the bros go, I was like, wow, people are going to love this thing.
- 30:53Yeah, that's really cool.
- 30:54You know, I actually don't know the answer to this question, but do you have some sort of like optimistic concurrency tracking?
- 30:59Like if two people pull it back, one person makes a change and saves it, the other one takes that object, tries to save it.
- 31:05What's the story there?
- 31:06There's a little bit of pattern that we have in there that I lifted entirely from what Hibernate does.
- 31:13There's a pattern using a version counter that I don't know that it's that widely used because it kind of interacts with the isolation level you're using in your transaction.
- 31:23I mean, really, the optimistic concurrency is something you can get in your database just if you set your isolation levels the way you want, if you set them higher.
- 31:34The version counter idea is that you load the row from the database and the row will have a special column that's the version and say the version is five.
- 31:41Do I have to indicate that?
- 31:43Sorry, do I have to indicate that specially on my class?
- 31:46Yeah, there's a whole, yeah, it's in the mapping option.
- 31:49So you have a column that you declare as part of the versioning and then the mapper will actually load that in that number or it could be a date stamp if you want.
- 31:58And it will add, when you go to update the row later, it will actually add that column to the criteria for the update statement.
- 32:06So it'll be like update row, set whatever, where primary key is blah and version equals five.
- 32:14And then if the update does not match any rows, that means version five is gone.
- 32:18It means someone else changed version five underneath you.
- 32:21And then they'll throw an error.
- 32:22And that's basically one simple way of doing optimistic concurrency control.
- 32:26It only applies to updates and deletes.
- 32:29It doesn't apply to inserts.
- 32:32Inserts is a whole different ballgame.
- 32:33People want to be able to insert some value.
- 32:36And if another thread tries to insert the same value, they want to use the one that's existing.
- 32:39That's a hard pattern.
- 32:40There's ways to do that in SQL, but none of them are automatic.
- 32:44You have to kind of roll it the way you want to roll it.
- 32:45Yeah, that's interesting.
- 32:46But I think even that level of updates is really a nice feature to have.
- 32:51Yeah.
- 32:52So you talked about the mapping a little bit.
- 32:54When I create these classes, basically what I do is I create a class that derives from some kind of declarative base instance I've created.
- 33:00And then I declare the columns.
- 33:03And how do I do that?
- 33:05So the declarative system is, again, that was a system that I added to SQLAlchemy back when I did version 5 and I worked for the Charlie Rose website.
- 33:13We didn't have any declarative system built.
- 33:15And we had a cheap one that was built into the library.
- 33:18And then we had a third-party thing called Elixir, which didn't quite do what I needed.
- 33:22So I wrote a new version, kind of the third version of the declarative system, which is basically you have this base class that you can inherit from.
- 33:29The original idea of SQLAlchemy was that you write – you would declare table metadata using just core.
- 33:35You have a table object.
- 33:36The table object has these column objects.
- 33:39The name of the table would match the table in your database.
- 33:42The names of the columns would match the names of the columns.
- 33:43They have little data types like a string or a date time or an integer.
- 33:47And then you would take your class, which would just be kind of a blank Python class, and say map.
- 33:52You'd map the class to the table.
- 33:56So the declarative idea is how most ORMs that we deal with today look, where you don't see those two separate things.
- 34:02You see kind of this class that has, like, class, my class.
- 34:05And then it has, like, id equals integer, and then first name equals string, and last name equals string.
- 34:11It looks like Django.
- 34:12It looks like every other ORM you see.
- 34:14So the declarative thing was added to do that.
- 34:16So you basically use the concept of a Python class.
- 34:19You say class, and then you my class, and then you put a colon.
- 34:23And then you indent, because Python, we're doing white space.
- 34:26And then you basically declare variables that refer to the columns that will be in your database table.
- 34:34And then as far as how that creates the database or the database exists already, that's kind of a separate concern.
- 34:39But you're basically defining what your schema will look like in line with your class definition.
- 34:46And it's a pretty common pattern that is familiar to everyone at this point.
- 34:50Yeah, I think it is a pretty familiar pattern.
- 34:52It's really nice.
- 34:53And it also ties into, when you're talking about doing the queries, those columns, basically, you can do operations on them, right?
- 35:01So I could, say, create, like, a query of book.
- 35:03And I'd say order by, you know, book.name.desc or descending or something like that, right?
- 35:10So they kind of play multiple roles, letting you do the queries as well as defining the schema and the constraints and all that, right?
- 35:16Yeah.
- 35:17So a funny story also is that it wasn't like that in the beginning.
- 35:21It used to be that you'd have your mapper and your class, but the class itself wouldn't have these attributes directly on it.
- 35:28It used to be you had to still talk to the table object.
- 35:30So if you had, you know, class user and it mapped to a table object called users, you would still have to query in terms of users.c.firstname.
- 35:39It wasn't actually on the class object because I didn't feel comfortable doing that for a while.
- 35:43And even though SQL object did do that, I kind of was hesitant to do that.
- 35:47But then once, I think maybe in version 4, I said, hey, you know, actually I can put the attributes on the class and have them be the actual column objects that we – they're actually not column.
- 35:58They're actually a wrapper around the column, but it looks like the column object.
- 36:01So, yeah.
- 36:01And then that led to this whole new thing called the hybrid property, which is like the fact that Python has this amazing symmetry in that everything is ultimately an object.
- 36:12If you have a class, it's still an object.
- 36:14So if you say myclass.foo versus an instance of myclass.foo, they look the same in code.
- 36:20And that kind of blew my mind.
- 36:23That was kind of a big discovery I made about Python like maybe five years into it that Python has this – I mean, I knew I loved Python's object model all the way through.
- 36:31But really, the hybrid thing really blew my mind.
- 36:35And there's a – I think I wrote a blog post a long time ago that I could probably point you to where I show – look at this hybrid thing where you can have a class with a method that does something at the class level and something else at the instance level.
- 36:46And it just was amazing.
- 36:47And I use it a lot on some subsequent jobs, and we did really amazing stuff with it.
- 36:51It's one thing to sort of realize that that feature is there.
- 36:54It's the other to say, let's use that to make a data access layer really work super well and make it – you define one thing, and it has this dual purpose, which is really cool.
- 37:02Yeah.
- 37:03There was a guy – I think his name was Ant Asma, and he was working on SQL for me a long time ago, and he first showed me a pattern that had that hybrid thing.
- 37:11And I was like, wow, and I really dove into it in the coming years, and I really realized it's a very handy pattern.
- 37:16Excellent.
- 37:17So you're talking about creating these columns and stuff.
- 37:20What if I've got a field that's – or a column that's not nullable or it needs a default value?
- 37:26How do I do that?
- 37:26Yeah.
- 37:27So the table metadata part – so say we're back in core.
- 37:29We're not doing over them again.
- 37:30So we're back in core – one of the goals of the core was that it would be this comprehensive system of building a whole schema from scratch.
- 37:38So meaning if you have a blank database, you can build up all of your objects in Python and then say, you know, metadata.createAll.
- 37:45And that will emit what we call data definition language, DDL, which we know as createTableStatements and createSequence and createIndex.
- 37:54It will emit all those things for you.
- 37:56So in order to make that feasible, we have to also have support for attributes like if a column is nullable or not, if a column is a primary key, if a column is indexed, if there's a unique constraint on a set of columns.
- 38:12So SQL Alchemist schema system has objects and flags that represent all of these concepts.
- 38:17And it's pretty comprehensive.
- 38:20And we have full support for all kinds of data types and numerics and floats.
- 38:25And we have server defaults.
- 38:28And we have every common thing except for triggers.
- 38:31We don't have a system for triggers.
- 38:34You can define your own system for triggers.
- 38:36And we don't have store procedures.
- 38:37So all the main things that you normally, your day-to-day database schema stuff like tables, columns, constraints, foreign keys, indexes, nullability, and defaults are all first class.
- 38:49Other things you can define like sequences we have.
- 38:52Other things like triggers and store procedures, you can define your own Pythonic little objects that will do that for you but more in a database-specific way.
- 39:02So anyway, when you do the ORM and you're doing declarative mapping, you're still using core column objects in your declarative mapping.
- 39:10So when you make a column object, you can still specify all the table attributes as well as you do your declarative mapping.
- 39:16So that's kind of the answers.
- 39:17They kind of meld together.
- 39:19And it was not that way in the beginning, but it evolved that way over the years to work that way.
- 39:22Yeah, it's really smooth.
- 39:23So I have these classes, and I can map them to existing databases.
- 39:28And you also said it emits DDL, which I can create my tables from, right?
- 39:32Yeah.
- 39:33That's just a function call, isn't it?
- 39:35Go to my declarative base.
- 39:36Was it create all, something like that?
- 39:37Yeah.
- 39:37Yeah.
- 39:38So the metadata object is kind of this collection of tables and other things, mostly tables.
- 39:44And you could say metadata.createAll, and you give it an engine to point at, like a database connection.
- 39:51And it will go on that database and see which tables don't exist and create all the tables that don't exist yet.
- 39:56That'll create them from scratch.
- 39:57What if I say add a column?
- 39:59Right.
- 40:00So the whole thing with adding a column and doing that, we call that migration.
- 40:03So, you know, as we go through this, it's funny.
- 40:06Nothing in SQLAlchemy was there in day one.
- 40:07Everything was like I had to figure it out and learn it.
- 40:10Like I actually did a talk for SQLAlchemy version 4, and they asked me in the audience, what do you do about database migrations?
- 40:15I'm like, well, I don't really know much about migrations, Automata.
- 40:18I used to do them.
- 40:19I do them by hand.
- 40:20So I don't really have a migration tool.
- 40:21So some people wrote a tool called SQLAlchemy Migrate.
- 40:25And that went pretty well for a while and is still in use today.
- 40:28And then I eventually wrote a newer version called Alembic.
- 40:32So that's kind of a separate package that you can download.
- 40:35And Alembic builds on top of SQLAlchemy as does SQLAlchemy migrate, which is still in production.
- 40:41And that defines a whole bunch of new operations on top of SQLAlchemy that are specific to being able to add columns, to alter the types of columns, and alter the nullability, you know, to remove columns, rename them, things like that.
- 40:54And my database migrations is a whole big issue unto itself.
- 40:58So it took me several years to really get my head around that, to, again, use that in production on the job because the best way to write open source code is to be, you know, what we say, eating your own dog food on the job.
- 41:09So I've used Alembic a lot.
- 41:10Alembic is now really widely used.
- 41:12It's very prominent within OpenStack, which I work for.
- 41:14And that's kind of the path to doing migration is kind of separate from SQLAlchemy core itself.
- 41:19It's kind of built on top.
- 41:20Yeah, that's cool.
- 41:22How about relationships?
- 41:24Like if I've got a bookstore and I've got categories and there's books in the categories and reviews about the books and so on.
- 41:29So that aspect of object relational mapping is really where object relational mapping starts to look like this concept that is really going to change.
- 41:40The way a relational database looks compared to the way an object jointed data model looks.
- 41:46So in relational schemas, if we want to have a table that refers like a row in this table, it's like say it's an account row or something, and it needs to refer to like some other related concept like an address.
- 42:01In relational database, we use something called a foreign key constraint, which means that we're going to – you don't have to actually use the constraint, but you would say that this particular column or set of columns in our account table actually refers to the primary key of a different table somewhere else.
- 42:18And that's called a foreign key.
- 42:20And the constraint in the database makes sure that the value you put in those columns actually exists in the remote table.
- 42:26So in ORM, ORMs have this concept of either making a one-to-many or a many-to-many or a many-to-one relationship, which models in object-oriented terms what that looks like in terms of collections.
- 42:41So if I have – in all of our tutorials, we have this concept of a user object, and the user has one or more email addresses, and it's a really old example, but I keep going back to it because I just know it really well.
- 42:53So your address table in your database will have a foreign key called here's an email address, and what user is this – what user is the owner of this address?
- 43:04It'll have a column called user ID or something, which will refer to the primary key of the user table.
- 43:09So the user table kind of refers to – I mean, the address table refers to the user table.
- 43:14In your model, you want to have the user object has a one-to-many to the address class.
- 43:23So it's kind of – if you define a one-to-many, it's kind of defining it in the other direction, like user points to address.
- 43:29But in your relational database, it looks more like address points to user.
- 43:34But it turns out that every one-to-many relationship in object-oriented land is the same as the many-to-one in the other way.
- 43:40So SQLAlchemy basically lets you make the one-to-many and the many-to-one at the same time using this concept called a backref.
- 43:49This is a little bit rambly, but if you look at our tutorial, it's laid out in a much more organized fashion.
- 43:56So, yeah, there's basically an object – there's a function in SQLAlchemy or called relationship where you associate it with a parent class and then you give it a target class.
- 44:05And relationship will make sure that the foreign key setup that is expected exists between those two tables.
- 44:13And it will kind of mediate that pattern on behalf of the object model to the database schema.
- 44:20Yeah, that makes it really easy to almost not even worry about that.
- 44:23So is that like a lazily evaluated thing?
- 44:26So if I pull back a book, it's not going to go pull back all of its reviews instantly unless I touch that.
- 44:31Right. So that was one of the patterns in Fowler's books called lazy loading, which is the concept that if you have an object and you have some kind of method or attribute that represents data from your database, you might have the object and it didn't actually load that other data.
- 44:45It's only when you touch that attribute or method that it suddenly goes, oh, I have to load this stuff and I have to go out to the database and load more information.
- 44:51That's an efficiency pattern called lazy loading.
- 44:53It means you can load a lot of objects and not have to go through all the time of loading all the related things.
- 44:59All ORMs do lazy loading, just every ORM I've ever seen.
- 45:03But lazy loading is also at the core of this somewhat controversial concept in ORMs called the N plus one problem.
- 45:12Right. That's probably the most common performance problem people run into is they don't realize they're doing this N plus one query.
- 45:19So I get one book or I get one book.
- 45:23So that's the one.
- 45:24And then I try to touch the other items or a set of books.
- 45:27I loop through them every time I touch that property.
- 45:29I'm doing one more query.
- 45:30So for N books, I do N more queries against the database, right?
- 45:34Yep.
- 45:35That's the N plus one problem.
- 45:36And every article ever written that is against ORMs will have that entire example as the cornerstone of its premise.
- 45:45So the other thing that SQL Alchemist had from day one, besides the unit of work, the other cool thing it had was this thing called eager loading, which means that you can, if you kind of know you're loading these users and you want the addresses also, you can say to the query, hey, load the user.
- 46:03But then also, can you also load the addresses at the same time instead of me doing the N plus one problem?
- 46:08And in that way, you will only do, if you do what's called joined eager loading, it will actually do a join, an outer join.
- 46:14It will load your user row.
- 46:16And while it does the select for the user row, it will do an outer join to the address table so that it gets zero or more address rows at the same time.
- 46:25And then the query object knows how to organize the product that we get back.
- 46:31Because if you do a join in SQL from one table to a many, you'll get the one row repeated.
- 46:37So the ORM knows how to deduplicate those rows on the left side of the join.
- 46:42And that's called eager loading.
- 46:44And one of the tradeoffs of eager loading is that when we write a query, we say, hey, Mr. Object Database thing we don't know anything about.
- 46:53We want users.
- 46:54And then later I want addresses.
- 46:55If I have to say, hey, I want users, but I'm also going to want in the future these addresses, that is what you would call a leaky abstraction.
- 47:05Because it means that the way the system is going to do its load, you need to tell it up front how to do its job to some degree.
- 47:12You have a little bit of awareness that this is not just like some weird LDAP database.
- 47:17This is actually a relational database.
- 47:19And these two things are in two different tables.
- 47:21One of the things SQLAlgmy embraced very early on, and one way that is very different from most other ORMs, is that it did not try to get around that issue by pretending the database doesn't exist.
- 47:34It's like, you know what?
- 47:35This thing is to automate our work.
- 47:38It's to make our lives easier and write less code.
- 47:40It's not there to hide that there's a database.
- 47:43So we need to be aware of things like if we want to load the 1 and also the N and not have N plus 1, we need to give the query a hint up front to do that.
- 47:53So that's ego loading in a nutshell.
- 47:56Yeah, I really like that philosophy that you guys take looking, you know, saying, look, we're not going to hide this from you.
- 48:01We're going to make your life easier and less error prone, but we're not going to treat you like an idiot or hide this stuff.
- 48:08Or, you know, you can still get to what you need to.
- 48:10That's great.
- 48:10Right.
- 48:11How do I do that in code?
- 48:12So what do I say on my query to say I want to eager load books and reviews?
- 48:17So modern SQLAlchemy, I mean, this is, again, APIs have changed dramatically.
- 48:22Modern SQLAlchemy, you would say query for a user class, and then there is a call on the query, which is a method chaining system.
- 48:30You say query.options, and then you add to the options function a bunch of constructs called join load or subquery load or immediate load.
- 48:40There's all these little directives we have that you can say, hey, query for user.options, join load user.addresses, join load address.related something else.
- 48:50And you can kind of give these directives in the options of the query as to what other things it should be loading onto the primary thing that you're loading from.
- 48:59Oh, very cool.
- 49:00You've been talking about the different versions of the API.
- 49:04How have you dealt with versioning that API over time?
- 49:07Well, I mean, it's been 10 years.
- 49:09And what I kind of did in the early days, you know, we were first 0.1, and then we were 0.2, and 0.3.
- 49:19And back around 0.3, someone wrote an O'Reilly book for us, and we're like, you should be 1.0.
- 49:25And I'm like, no way.
- 49:26We are so early.
- 49:27Like, this thing is totally not at all how it's going to be.
- 49:30So with 0.1, first of all, I didn't even publicize the library at all in those early days.
- 49:36I just maybe announced it on one mailing list.
- 49:38And people just came, kind of just came and started using it.
- 49:42They started using it in production.
- 49:43And 0.1, 0.2, I made enormous, enormous changes that just totally ripped the rug from everybody.
- 49:50But we were 0.1.
- 49:51Sure.
- 49:51Did it make you nervous to hear that they were, like, so heavily dependent on this new thing you created?
- 49:56People got way too dependent too fast.
- 49:58And in early days, I think Reddit started using SQLAlchemy, maybe version 0.4 or 0.5, you know.
- 50:06And there were some messages from them years ago.
- 50:09I don't know where they were, so whatever.
- 50:11Where they were kind of complaining about SQLAlchemy changing its API too much.
- 50:15And really, the API, I made the biggest changes before version 0.5.
- 50:210.5, we really started settling down to this new kind of thing with the query, with the attributes linked to the class.
- 50:29And then we had to keep the API changes all the time.
- 50:33And from 0.5 on forward, even from 0.3 and 4, I always made sure that there was a backwards compatibility path.
- 50:40I made sure that everything that changed was always announced.
- 50:42And if something happened that was unexpected, that was a regression, and we would address it.
- 50:48And it was almost never that I said to someone, you're stuck.
- 50:52You'll have to just change your code now.
- 50:54Like, I always made sure that there was a backwards compatibility path.
- 50:57But it meant you had to kind of, especially in the early days, it meant you had to really kind of watch when we did the next major number, which is really the minor number.
- 51:04But we call it the major number.
- 51:06You needed to watch what was changing.
- 51:08You needed to test your stuff ahead of time.
- 51:10Things are better now.
- 51:12Now that we have pip is much better.
- 51:14And we can put up beta releases that won't automatically install for people.
- 51:18And I always put very, very, very comprehensive migration notes for every version.
- 51:25You can see them going back on the site, probably back to version 0.4 or 0.3, where I say, here's the things that are going to change.
- 51:31Here's the things to look out for.
- 51:32Here's the things to test.
- 51:33And after version 5 and version 6, version 7, it changes less and less each time.
- 51:38People went from version 7 to 8 with very few hiccups.
- 51:42You know, 8 to 9, 9 to 1.0.
- 51:44There's really nothing you have to change in your code anymore.
- 51:47We like people to test because it might be surprises that we didn't expect.
- 51:51And we fixed those.
- 51:54But APIs very rarely break in any kind of backwards incompatible way.
- 51:59The only time it's a problem is that if there's something that was broken in SQLAlchemy 8 or 9 and people were relying upon the broken behavior.
- 52:07That's the hardest one is when people rely upon broken.
- 52:09And they didn't know it was broken, so you can't blame them.
- 52:11When we fix things that were broken, we'll usually make it raise a warning in the current version they're using.
- 52:18Like 0.9 has something that's broken.
- 52:20We'll have it admit a warning, so they'll update, upgrade to 0.9.9.
- 52:24And all of a sudden they see these warnings.
- 52:25It'll say, this will be an exception to 1.0.
- 52:27So they'll kind of know to change it there.
- 52:29But you do need to be watching the versions and the changes.
- 52:32I've worked really hard to make all the change logs very easy to find on the site.
- 52:37I mean, other projects I can never find what changes.
- 52:40It's very hard to find.
- 52:41I think our site is very transparent, and you can see everything happening in many ways.
- 52:47And it's very highly linked as far as the development log.
- 52:51Yeah, that's excellent.
- 52:52It is super comprehensive.
- 52:53So one thing I think is interesting is maybe talking about some of the notable users of SQLAlchemy.
- 52:59I know you've talked a little bit about a couple of them.
- 53:02But you've got a whole list at sqalchemy.org slash organizations dot html, right?
- 53:07Yeah.
- 53:08So, yeah, I think I first saw MongoDB was doing that.
- 53:12They had this enormous list of people using their stuff.
- 53:14And I'm like, I want that, like a big wall of honor of all my – when some big company uses your product, it's like, wow, I'm important.
- 53:23I did this important thing.
- 53:24So up there, there's a lot of big – Reddit was the huge one.
- 53:29Reddit only uses the core.
- 53:31They don't use VRN.
- 53:32Their code is very unusual in the way they do things.
- 53:37And I'm glad they don't use the ORM because they would really be killing it.
- 53:40Yelp uses a lot of SQLAlchemy.
- 53:42I've been – I visited Yelp.
- 53:43They use the ORM a lot.
- 53:45Uber, you know, uses SQLAlchemy.
- 53:48Hulu.
- 53:50Dropbox is there, right?
- 53:52Yeah, Dropbox.
- 53:53Yeah, Dropbox is another one that uses the core.
- 53:55They're another – you know, they actually wrote some blogs that were a little bit critical of the ORM, which I was kind of annoyed about.
- 54:01But they use the core still.
- 54:02They use it.
- 54:03Yeah, a lot of animation studios use it.
- 54:08There's a lot of companies that use it who I know use it, but I don't really exactly have permission.
- 54:14You know, one of them is, you know, kind of a Mickey Mouse operation anyway.
- 54:19A lot of companies that are up there in organizations, you know, Fedora uses it.
- 54:23And, of course, now I work for Red Hat and I work for OpenStack.
- 54:26And OpenStack is this cloud computing platform that's open sourced.
- 54:30And a lot of companies like PayPal and IBM and AT&T use it.
- 54:34And that thing uses SQLAlchemy all throughout.
- 54:37It's super heavy SQLAlchemy throughout OpenStack code.
- 54:40So it's a very widely used library.
- 54:42Yeah, that's really excellent.
- 54:43You must be proud of it.
- 54:44Yeah.
- 54:46Cool.
- 54:47And you should be.
- 54:48So what about the whole async movement with, like, Twisted and AsyncIO?
- 54:54How does that play with SQLAlchemy?
- 54:56So the async thing is something, another Python story where it's something that I wasn't really that interested in.
- 55:03But it's a big deal in Python, the async.
- 55:05Because when I first came to Python, the two, you know, 800-pound gorilla things that existed were Zope and Twisted.
- 55:12You know, the old school, you know, the old guard of Python is Zope and Twisted.
- 55:17So Twisted is great.
- 55:18It's still out there.
- 55:21Async, the problem with what we call explicit async is that it's not really compatible with this idea of lazy loading that we see Martin Fowler first described.
- 55:31The concept that when you access something on a class, it's going to automatically, behind the scenes, go and fetch data from a database.
- 55:39It means that it's going to do I.O.
- 55:42And when you do things with the async model, the async model is all about that input-output operations should never be implicit.
- 55:51They should always be something that you code into it and say, hey, this is about to do I.O., so please stop what we're doing, do the I.O., and then let the whole interpreter go and do something else.
- 56:02And we do that in the newest version of Python's async I.O., which is in Python 3, you do a yield.
- 56:09And it's very slick, but it still is a statement that is an interruption in your code.
- 56:14Right.
- 56:15You have to explicitly say, I'm waiting here, I'm waiting here, I'm waiting here.
- 56:18And at any of these points, you can take that thread and go do something else with it until I'm done talking to the database or something, right?
- 56:23Right.
- 56:24So people have wanted SQLAlchemy to work a twist for a long time.
- 56:28I've looked into it.
- 56:29I actually had some clients pay me to look into it.
- 56:32And every time I look into it, it's like, eh, it's really not worth it because we have to completely rewrite the whole core engine part of this thing to be this kind of inside-out callback routine.
- 56:44This is with classic Twisted.
- 56:46How about the async I.O.?
- 57:16Right.
- 57:17So with async I.O., there's a big blog post I just wrote in the past couple of months that's about – it's called Asynchronous Python and Databases.
- 57:24And it's gotten a lot of views.
- 57:27And it kind of sums up the whole situation.
- 57:29I went into async I.O.
- 57:31I looked at it.
- 57:31I think it's really great.
- 57:32And it's definitely a huge improvement.
- 57:34Async I.O., I actually could write – a lot of SQLAlchemy could be made to work very seamlessly with async I.O.
- 57:41In that it wouldn't require that much code to be replaced.
- 57:47What's nice about async I.O. versus the older callback style is that there's kind of some ways you can have the async thing be explicit on the beginning of where a method does something.
- 57:57And then up the call chain at the end.
- 57:59And you can kind of get away with the calls in the middle not knowing that they're doing async somewhat.
- 58:05There's a little bit of Python syntax helping here.
- 58:08So I looked into how I could make the core do async I.O. for real.
- 58:13And actually, it can be done.
- 58:14I started writing parts of the pool.
- 58:15And it would be a lot of work, but it could be done.
- 58:18But in this blog post, I wrote, like, do we really need to do it?
- 58:21Because when you use async packages, even when you use Twisted, and Twisted has been doing this from day one for databases,
- 58:28when you have code that really works better with threads and traditional, you know, preemptive, not preemptive, but traditional threading,
- 58:34you throw that code into what's called a thread pool.
- 58:38And then the asynchronous part of your at library can actually call into routines that are invoked synchronously within a thread pool,
- 58:46but they are given kind of an asynchronous facade.
- 58:48And I think that is the best way to do it.
- 58:51If you're writing a lot of database code, you really don't need to worry.
- 58:55You really shouldn't have to be worrying about deferring to I.O. within the database,
- 58:58because you really, in the database code, you're in a transaction.
- 59:00You definitely need to do A, B, C, D, and E.
- 59:03It's not really a mystery when the I.O. has to happen.
- 59:06It's very kind of boilerplate-y.
- 59:08And one of the entire reasons that SQL Alchemin exists is to reduce the need to type repetitive boilerplate.
- 59:14That's the whole point of it.
- 59:15You don't need SQL Alchemin to do anything.
- 59:16You can do it all by hand, but the point is to reduce boilerplate.
- 59:20So to that extent, I think you're better off putting your database code in a thread pool,
- 59:25or using a system like eventletg event that is an implicit system.
- 59:29And the blog post really goes into this in great detail as to show the benchmarks, why I think it's better.
- 59:34And I think a lot of people agree with it.
- 59:36Cool.
- 59:37Okay.
- 59:37You'll have to send me the link to that blog post.
- 59:39I'll put it in the show notes.
- 59:40Excellent.
- 59:41So I think we've been talking for a while.
- 59:43It's probably a good place to wrap up the show.
- 59:45And a question I ask the guests on my show on the way out the door is, you know, there's a ton of packages on PyPy and out there in GitHub and stuff.
- 59:53What ones are notable to you?
- 59:54What ones are your favorites?
- 59:55Seek walk me, of course.
- 59:57Yeah.
- 59:58On PyPy?
- 1:00:00What's your favorite PyPy package?
- 1:00:02I use pytest quite a lot.
- 1:00:04I recommend pytest.
- 1:00:06I recommend the mock library, which is actually part of the Python standard library now.
- 1:00:10Mock and PyPy are the ones I see myself downloading most of the time.
- 1:00:17You know, what else?
- 1:00:19There's probably others, but they're not coming to mind at the moment.
- 1:00:22I'd have to look and be like, oh, yeah, I use that thing a lot.
- 1:00:24You know, I mean, obviously, NumPy is the big one.
- 1:00:27Yeah, NumPy.
- 1:00:28Yeah, NumPy is good.
- 1:00:30Like I said, I think Sequel Alchemy is certainly one of the top ones out there.
- 1:00:34Yeah, yeah, it is.
- 1:00:36So before we call it a show, is there anybody or a thing you want to give a shout out to and call attention to?
- 1:00:42Oh, well, my cats for helping me with the show today.
- 1:00:46And, you know, just I'm really appreciative of the people that come to see my talks and the people that talk to me on Twitter and tell me how great I'm doing.
- 1:00:56And even the people that, you know, naysay me as well.
- 1:00:58I get good feedback from them.
- 1:00:59So I'm really glad that everyone is kind of following along with what I'm doing.
- 1:01:03Excellent.
- 1:01:03Can people come and contribute to Sequel Alchemy?
- 1:01:05It's on Bitbucket, right?
- 1:01:07Yeah, I mean, it's a big code base and people do contribute all the time.
- 1:01:11I get pull requests all the time.
- 1:01:12through both Bitbucket and GitHub and you can send a pull request.
- 1:01:15We haven't done any sprints in a long time.
- 1:01:18I mean, that's something that we could do again if I had people to help me with that.
- 1:01:21You know, and people in the OpenStack community help out.
- 1:01:25So it's a very open project.
- 1:01:27If you go to the website, there is a toolbar that has community links and it has a link called Develop.
- 1:01:33And it'll show you all the ways that you can get involved with helping develop Sequel Alchemy.
- 1:01:36Awesome.
- 1:01:37Mike, thanks so much for being on the show.
- 1:01:39It was a really interesting conversation.
- 1:01:41Yeah, thanks for having me.
- 1:01:42You bet.
- 1:01:44This has been another episode of Talk Python To Me.
- 1:01:48This is your host, Michael Kennedy.
- 1:01:49I want to say thank you for listening.
- 1:01:51And if you want to support the show, be sure to check out our Patreon campaign at patreon.com slash mkennedy or talkpythontome.com slash sponsor.
- 1:02:02Remember to check out the show notes at talkpythontome.com where you can find all the links from the topics we discussed in the show.
- 1:02:09And before you go, take a moment to become a friend of the show.
- 1:02:13Visit talkpython.com, click on friends of the show, give us your email address, and you'll be in the group.
- 1:02:19Smix, take us out of here.
- 1:02:20Stay tuned.
- 1:02:40Bye.
- 1:02:41.