Two frameworks that have taken the Python world by storm lately are FastAPI and Pydantic.
Once you already have your data exchange model in Pydantic, you might want to use that code for
storing or talking to your database. And if you have database models, you might want to somehow
use those models to power and document the APIs you're already building with FastAPI.
But the popular ORMs, such as SQLAlchemy and others, far predate Pydantic. But could those
two things be put together? Sebastian Ramirez is here to tell us the answer is yes. We're
covering his project SQL model, which is the marriage between Pydantic and SQLAlchemy.
This is Talk Python2Me, episode 353, recorded January 17th, 2022.
Welcome to Talk Python2Me, a weekly podcast on Python. This is your host, Michael Kennedy.
Follow me on Twitter where I'm @mkennedy and keep up with the show and listen to past episodes
at talkpython.fm. And follow the show on Twitter via at Talk Python. We've started streaming most of
our episodes live on YouTube. Subscribe to our YouTube channel over at talkpython.fm/youtube
to get notified about upcoming shows and be part of that episode. This episode is brought to you by
datadog and tonic.ai. Please check out what they're both offering during their segments.
It really helps support the show. Transcripts for this episode are brought to you by Assembly AI.
Their speech to text APIs automatically transcribe and understand audio and video in just a few lines
of Python code. Check them out at talkpython.fm/assemblyai.
Sebastian, welcome back to Talk Python2Me.
Thank you very much. Thank you for inviting me. It's a pleasure to be here again.
It's great to have you back. When you were on the show, we were talking about FastAPI,
and it seemed like so much had happened. You've done so much, and now there's like all these other
frameworks that you've built and all sorts of exciting things, right?
Yeah. Very, very exciting stuff. It's like very exciting because Python is getting so excited.
Well, it's very exciting. It has always been exciting, but there's so many new things that
it's great to build things on top of.
It's interesting. I feel like your frameworks, more than many,
take advantage of and almost depend upon the latest aspects of Python.
Yeah, yeah, absolutely. Like, someone made a meme at some point on Twitter about, like,
this guy that was like, pen, pineapple, apple pen. And it was like, type annotations and another
library and just put it together. And that's what I'm building. And yeah, it's pretty accurate.
Yeah, absolutely. You know, there's, as a community, we sort of muddled our way through the Python 2 to 3
transition, and it took a lot longer than even Guido and everyone, many other people expected it to
take. But now that we're on the other side of it, stuff like what you're creating and other people are
creating, that's what would have been possible had we gone sooner, right? But now it's like,
no, everyone's putting their effort into this, these new ideas and these new aspects that are now
possible.
Yeah, yeah.
I think it's great.
Yeah, absolutely. And I feel like the way Python is growing and improving is amazing. Like, you know,
there are some growing pains, like as with any project or with anything, but like, so like,
it's been able to grow in the directions that are needed and to support all the things that's
what users are needing. And like, we can do very cool stuff that will not even be possible
in other languages. And it's, I don't know, for me, it's pretty exciting.
Yeah. It's the same as for me as well. It just gets more exciting. You know, you could see it,
you just keep working the same thing. And it just keeps, ah, well, I've been doing that for a long
time. I need to change, but I don't feel that way at all. I feel like every day there's something new
and amazing and still the possibility for more incredible things to come is certainly out there.
I don't feel like we've hit the limit of what it's possible for framework authors like you to build
or for the core devs to make Python do, you know, there's the whole performance resurgence thing
that Guido Van Rossum and Mark Shannon are doing, that Sam Gill did, that Anthony Shaw is doing,
and others. Yeah. I think there's, it's good, right?
Yeah. It's amazing. And I feel like the, how the, the energy between the community and the
core developers and like editors and all the tooling, like all growing together and all supporting each
other, like helps each other part to grow more and better. And it's so exciting, you know,
to get like, for example, like support for very recent things and have been able to use them
right away in editors. It's like, oh, so cool. It is absolutely cool. And yeah,
the editors are definitely coming along as well. Now, before we get into your latest project,
SQL model, which is very exciting, let's just get a quick update on you. You know,
you told us the story, how you got into programming Python before, so we're not going to ask you that
again, but what have you been up to since you were on the show last? So when I was on the show last,
we were talking about FastAPI, right? And the cyber already existed if I'm wrong, right?
Yeah. I think that those were the two things you had built and FastAPI was...
Okay. Okay. Yeah.
Had FastAPI made it to the top three web frameworks yet? I'm not sure if it had, but it was,
it was right around that time. Yeah. That's incredible.
I think it was very close to that point. That was mind blowing that people were being able to use it so
much and adopted so much. And like when it came out, out in the service like that, it was super cool.
Yeah. It was, yeah, super exciting. And yeah, like, I don't know, like I have been, you know,
like I have been trying to focus, like I have always been trying to focus on whatever is the next
thing that I can work on that will have the biggest deep pipe that can the most. And I ended up just like
changing areas and like trying to improve different areas and different things. And recently that I was
working with the SQL databases, well, or recently, I don't know, some on-sable, that I was working
with SQL databases and I was working some of the existing libraries and I wanted to have like all
the benefits of the new features of Python, but I wasn't able to have like as much things as I could
because most of these libraries were built before we had all these, all these new features.
So I wanted to be able to get that. And I figured that the best way to build it was applying the ideas
that I had and the learnings that I had from the other tools and from the other things that just like
put the thing together because there were some libraries that were trying to do similar things, but like
I felt like there was still a bit more that could be done. So yeah, I was just like trying to get that.
And that's how we ended up starting.
Yeah. Fantastic. Now I feel like SQL model, I don't know for sure I'm asking you, but it seems to me
looking in from the outside that SQL model was something you're like, I need a good ORM
or FastAPI. And it, the things out there didn't click for you in the way that you wanted it. So
you're like, I'm going to build something that fits with this. Right.
The good thing with FastAPI is that it doesn't have any need for tightly coupling it with any ORM,
with any database. So it can be used with anything, but still there are some, some things that might not
be as convenient in the ORM itself. Like if you use it alone. And for example, with FastAPI, then you use it to
declare all the data models, all the shapes of the data that you want to receive and you want to send
back and to do like all the data validation, documentation, serialization. Then you declare
a bunch of those data models with Pydantic. But at the same time, you will end up declaring,
duplicating a lot of that information in a separate ORM just to connect to the database and to handle the
database and stuff with Python objects. But then you have to duplicate the information in two different ways.
And that's what was like, it was not the best developer experience, I guess. And I was trying to
make it a bit more user friendly, a bit more developer friendly, I guess, to work with databases and data
models and avoid all that duplication information. And at the same time, make it as easy as possible
to write a code, just using the same standard type annotations and just using the same, the same
intuitive things that we can already use. And that's the point that I was trying to hit.
For people to see how that clicks together, I know 12% of the community who builds web APIs and
frameworks is using FastAPI, but there's a decent percent out there who maybe haven't heard or
looked into FastAPI. Maybe they've heard about it, but they don't know the pieces. Maybe given that
that's some of the motivation, maybe give us a sense of how do you build data models that match your APIs
and how do you do things like generate the open, the swagger documentation and stuff like that, like
set the stage for why just straight SQLAlchemy or some other standard pony ORM or something like that
didn't just directly map over to how FastAPI works. Awesome. So like just a very quick intro to FastAPI,
it's a web framework that is focused a lot on building web APIs. The main idea is that using
the standard type annotations or type hints, so the way that you in a function you declare what is the
type of some particular variable, using that same information, that same information by default will
give you some certainty that the code is correct and will give you the completion and the inline errors
in the edit. FastAPI uses that same information to do data validation of the data that you receive in
the web API and to do the serialization of the data that you are returning back and to do automatic
documentation. This is all based on a bunch of standards, open API, JSON schema and a bunch of
other things. And because it's based on these open standards, then it can generate and it can also
provide Swagger UI, as you were saying, which is this web user interface that shows all the information of
the API. All the endpoints, where are the data, the shapes that you can send, and you can actually interact
with the API directly from the routes without having to go to some documentation site and then update and
the wiki gets updated and things like that. Yeah, absolutely. I built a weather service API for one of
my courses. This has limited data. People don't try to use this for actual weather service. But it's a
FastAPI API. And in addition to all the other cool things it does, like quickly generate the stuff it needs
to, you can just go to slash docs and it'll give you the schemas. It'll give you API endpoints, the values
that go in, the return value, all of this. I guess the most important aspect of this is probably Pydantic,
correct? Yes, absolutely. Like the most important part. You define your stuff in Pydantic models and
then that drives so many of these things. Yeah. So FastAPI is built on top of two tools. Pydantic does
all the data stuff, data validation, serialization, documentation, and Starlette does all the web stuff.
FastAPI just puts them together in a way that they work together. And that's some extra things on top.
But Pydantic is the thing that powers all this data validation and all this automatic documentation.
Right. Pydantic is so based on the same type annotations, the standard Python type annotations. So you
can just like use the same intuition that you will have for a standard Python and they get all these
data processing. Yeah. And FastAPI does several things with the Pydantic models. It does model
binding, I guess I'll call it. That term's not super common in the Python world, but you can just say
my API function or web function takes this model and then FastAPI will like create the Pydantic model and
set the values and do the validation. And then also the return value, you can say it will drive this
documentation and so on. So the reason I wanted to set the stage so much around Pydantic is that's one of the core
elements of SQL model, right? And not just using that library, but so that it can be used as the models in
FastAPI, right? Yes, exactly. So SQL model is a library, what they usually call an ORM. And if you don't know
what an ORM is, it's just a library to connect SQL database with Python objects and classes. I don't
know why we use the term ORM. I feel it's a bit abstract, but it's just a library to connect SQL
databases with Python objects and classes. And the thing with SQL model is that it does a lot of work
inside so that each model that you create is already a Pydantic model. It's not that it internally uses a
Pydantic model or internally creates some additional Pydantic model is that each model is itself a
Pydantic model. And at the same time, SQL models is built on top of Pydantic for data processing,
validation, all this stuff. And another library that does all the work to communicate with SQL databases,
which is called SQLAlchemy. And each one of these models is both Pydantic and SQLAlchemy.
Yeah. It's an interesting marriage between Pydantic and SQLAlchemy. Much of the way that you work
with it would be very familiar to people who do SQLAlchemy today, right?
Yes. That's the idea that it will be very familiar for people that is already working with Pydantic,
probably because they are using Plastic BI. But at the same time, it will be very familiar for people
working with SQL model because it's just the same look and feel. And it's indeed a strange marriage
because these two libraries are so different that getting them to connect and work together in the
in the very different ways they are built. It was very, very strange. But they actually ended up like
working quite the way. Yeah. I imagine that it was pretty tricky. You know, anytime that you get in
the middle of an ORM and its model, I've tried to do that with other frameworks and said, oh, it would
be great if I could say use inheritance in this way on my model so that there's not duplication. Like,
oh, no, no, no, you can't do that because the thing really depends upon the exact class that
derives from its sort of like ORM base class. That's what it uses for its determining what columns are
there and so on. Right? Yeah. I mean, it was so crazy. I spent so much time in the
the book that we were trying to figure out what was happening underneath and studying so much about
like the black magic in Python, the stuff that I always fear, like all the meta classes and stuff
and all that weird stuff. I studied so much of that to be able to mix these things together. But,
yeah, like because they do things in a very different way. At the same time, that's facilitated,
allowing one thing to do its job and the other thing to do its own job in their own particular ways. So,
yeah, it was fun. Yeah. Very cool. Whenever I think about an ORM, the thing that I first go to focus on is
the Python classes. Because for me, the whole point of the ORM is to let me talk to my database
through those classes and model my application through those classes. Right? So let's maybe get
started by talking about how do I create a class, a model, a SQL model model here that is both a
Pydantic model and a SQLAlchemy-like model. I'm going to talk us through what does it look like?
Cool. From SQL model, you will import this class, SQL model. And SQL model, you inherit from this class.
You can, for example, create a class hero. And then let's jump to the internal parts of that. Then you
will define some attributes for this class hero. For example, you could say that it has an ID,
that this ID will be an integer. The way you declare that is with standard Python type annotations.
You could say that it has a name and it's a string. If you are familiar with Pydantic, it basically
could exactly be a Pydantic model. Yeah.
The way you declare your ORM. Yeah.
In the simple case, right? Yeah. Yeah, exactly.
If it really is just an integer and it just has a number, you don't have to make it auto-increment or
any weird stuff like that, right? Exactly. In the simplest cases, it will be,
it will look just exactly like a Pydantic model. And in fact, it will be a Pydantic model. And then for
some particular cases where you need to add a little bit of extra information to tell SQL
model and SQLAlchemy underneath to tell it, "Hey, this does this thing with the database," then you can
pass additional parameters and additional configurations. So for example, when you create
the ID of this class, this will be the ID of the table and it has to be a primary key. So then you can
use the function field to say, "Hey, this still has a default value of none, but I need this particular
field or this particular attribute or this particular column, however you want to call it. I need this to
be the primary key." And then that information is passed through to SQLAlchemy underneath, which is the one that does all the work.
And there's something particularly interesting here is that you are saying, "Hey, this has a default value of none,
and that none default value will be used by Pydantic in the Pydantic side of things, but at the same time, it will be
used in the SQL side of things. So in the database, this will have also the particular default value.
In the case of the primary key, it's just because when you create a model, you still don't know what
the primary key is until you say." Most of the time, the database generates that. You could say
your primary key could be your email address, but it's common to have it just auto-generated by the
database. Yeah. A UUID or auto-increment integer or something like that. Exactly. Yeah.
For those cases, you want to have the type annotations very precise so that your code can tell you,
"Hey, this could be known at some point." That's just a particular detail. But the thing is,
the important thing is that you use standard type annotations to declare attributes, and then these
will be mapped to the data model in Pydantic, but at the same time will be mapped to the table in the SQL
data. Nice. So it kind of behaves in the two ways. And that means that what you put into your database is pretty much what your API model is as well, right? Exactly.
This portion of Talk Python to me is brought to you by Datadog. Are you having trouble visualizing latency
and CPU or memory bottlenecks in your app? Not sure where the issue is coming from or how to solve it?
Datadog seamlessly correlates logs and traces at the level of individual requests, allowing you to quickly
troubleshoot your Python application. Plus, their continuous profiler allows you to find the most
resource consuming parts of your production code all the time at any scale with minimal overhead.
Be the hero that got that app back on track at your company. Get started today with a free trial at
talkpython.fm/datadog or just click the link in your podcast player's show notes. Get the insight you've been missing with Datadog.
That's the idea. Like in the most basic situation, the cool thing is that with this approach and with this tool, you can then create additional models that don't map to one particular table in the database,
but are just for handling data with the API. For example, if you create, if you have an API that receives data to create a user, it will probably receive a password from the user. It will have the username and the password.
And you want to be able to have that information in the model that you want to receive in the API. But you don't want to save a password as plain text in the database.
You don't? Isn't that the easiest way? I get these warnings from these various sites like, oh, your password can't be more than eight characters long because no, yeah, please don't save it in the database. That's a really interesting scenario, right? You need to receive it on one end, but you must not put it into the database.
Exactly.
You must, it should not carry on.
And for example, and then in that same situation, you create the user and you want to return the information of the user back to whatever is the client. You don't want to return the plain text password. You want to say, hey, this is the username, but like, that's it.
Yeah. Yeah. Probably not. It's very unlikely that you want to return the hash as well. You just, you don't want it to return at all. Right?
Yeah. How do you handle that?
Yeah. For these particular cases is where SQL model will shine because you can create one base model that will have like all the base attribute scores.
I'm probably will have the name, the last name, the address, the email, blah, blah, blah.
And then you can inherit from that model and they have different models for the particular use cases. For example, for creating data. So you will have a plain text password and for returning data, you will have no password at all. But then one of these models will be the actual model that stays in the database. The one that reflects the information in the database. And this one is the one that will have the hash password. But you didn't have to duplicate all the information for the model because they only inherit from the same base model.
Is that the section that I got on the screen here that says multiple models with FastAPI?
Yes.
Like how you do that?
Yes, exactly.
Yeah. So the idea is obviously you have got some shared information about the user, like the email and their name and stuff. You want to share that probably their ID, but you don't want to, you don't want to share, say there, like you said, the password or whether or not they're an admin on the site or those kinds of things you probably don't want to exchange over the API. Right?
Exactly.
Exactly. And if you need to duplicate all the information for each one of these particular models, there's a high chance that at some point, whenever you refactor the code, some part will be out of sync. And then you will have a bunch of errors and a bunch of bugs that are very difficult to detect. When you have duplication of code and you have to synchronize it by hand, it makes, it creates a lot of potential bugs that are very difficult to detect.
Yeah. So in the way you do your models, this is pretty neat. One of the things that you do is you've got your model hierarchy, you've got SQL model, which is the base class of all the things that interact with, well, SQL model.
And those are typically the classes that you create that would be like SQLAlchemy or Django ORM models. But in your world, you can have inheritance. And then somewhere in that hierarchy, you set table equals true as you create the class.
So it's not necessarily that just, oh, you derive from this class. So that's a table. It gives you more flexibility and go, this part is a table. That part is a table. Like in the scenario we're talking about, you have a base user where there's a name and a password, a hash password and stuff.
No, sorry. You wouldn't want to put that. You would put like your shared stuff into the base class.
And then you, right.
Name, last name, address, email, I don't know.
And then the thing that derives from a user would derive from like user base, which would say like table equals true and it could have its secrets there.
Exactly. Exactly.
Okay.
So that makes a lot of sense. Inbound. What about outbound? So I've got a FastAPI endpoint, right? It could even be Flask or whatever, right? And I've done a query to the database and I get the table version that has the secrets.
I can easily go to FastAPI and say the response model is the base thing. So the documentation is right. But if I go to the object I got from the database and I say as dictionary or to dictionary, I forgot exactly what the right term is, but the thing that sends it back, it's going to include everything in it, isn't it?
So this is one of those particular details of FastAPI that I think people in many cases miss. And is that in FastAPI, you can say, hey, this is the response model. So this is the model that I want you to use for the data that I'm sending back.
So this is the most obvious result of that is that in the automatic documentation, you will get the schema of what is the response data. And that is like the most obvious and visible. But FastAPI will also use that same model to filter out the data.
So if you say, if you say the response model is user out, for example, and the class user out, which is a Pydantic class or something like that, this class user out doesn't include the hash password.
From the function, you can return an object that includes the hash password or a dictionary that includes the hash password. But FastAPI will omit that field. And FastAPI will only return the particular fields that were defined in the response model that you say that will be returned.
Okay. I did not know that that also affected the outbound data, not just the documentation. That's pretty interesting.
Yeah. And in fact, that's in many cases, people ask, why does FastAPI use this parameter response model instead of using the return type annotation?
Because in Python, when you create a function, you can define what are the types of the parameters that the function receives. And you can also define what is the return of that particular function.
If FastAPI use the return value, but then you can say like, hey, the return value is this user out, but then the object that you were returning from that particular function was different object.
Then the editor will complain, the tooling and the tools that detect those typing errors like mypy will complain and they will detect, hey, you're saying that you're returning something, but you're returning a completely different thing.
So that's the reason why their return type is not what is used to extract that information.
And instead it uses this particular configuration responsible because it's used for filtering data.
Right. Okay. Interesting.
So for people who don't know, haven't seen this in action, you put a decorator like an app dot or API dot get, for example, just like you would say in Flask or something.
And you say, here's the URL, but then you also may put response model equals some Pydantic type in FastAPI.
And that drives a Swagger documentation.
And I am learning now drives the filtering of the allowed return values as well, which is pretty excellent.
Yeah. In fact, it will also validate that.
So if you are saying, hey, this will return this data and then whatever you're returning doesn't include that.
That will actually be an error on the server because you are saying that the contract is that I will return this data, but then suddenly you are not returning it.
Then it will raise an error inside of the server and it will tell you, hey, the data that you're sending is incorrect.
So there's something going on here.
There's something wrong with your code because you're sending something invalid from what you say that you were going to send.
That's pretty fantastic.
Okay. I didn't realize it made such great use of that response model.
So that's just a whole nother level to bringing Pydantic into that world.
So there's a bunch of comments and thoughts here in the audience.
So I kind of want to bring some of them in because there's a bunch of great ones.
First of all, former guest Waylon says your mustache is fabulous, which is always required when you're on a video.
Thank you very much.
Sveta Link says big like on FastAPI.
I think FastAPI is absolutely a good thing.
Hey, thank you.
And Jacqueline also very much.
Poplin says, I have a question.
If the data schema is complex and has nested JSON structure, in what case would you validate?
Like it's pretty straightforward to just nest the Pydantic, but this brings us, you know,
if you're going to be in a world where you're nesting Pydantic things, you may want to save them to the database.
What's the story on relationships?
And this basically, I've received some data that is like nested related data.
What do I do in SQL model?
So if you need to receive some complex data structure and you need to extract the information,
you can declare, you can declare models with Pydantic or with SQLAlchemy, sorry, with SQL model,
say that, hey, this is just a data model.
And then you can manually extract the sub components and then just add them to the database independently or something like that.
There wouldn't be a straightforward way to say like, hey, I received this giant JSON and automatically generate a bunch of different models that don't exist yet or something like that.
Or to automatically infer where to put each information.
It will be as straightforward.
Like it will have a lot of different design possibilities.
So it will be easy to get it drawn.
So the way that you will do it is that you define the complex data shape that you want to receive.
And then once you take it, you just extract it, each part of the information and each particular object or each particular data point that you want to then say to the database.
Now, to return data to the user, with SQL model, you can have relationships and relationships between different tables,
have like automatic joins and all that stuff.
This is all thanks again to SQLAlchemy, which is the thing that works underneath.
It already models that, yeah.
Yeah, exactly.
But then you can use that information and you can just like declare the models.
And this again works well with this idea of having narratives to be able to declare, hey, I want to return this model and I want it to include this particular relationship model.
So it will include the information from other tables.
We will just extract that information and return it to the client.
That's really cool.
What about lazy loading?
I'll ask this in two aspects.
If I've got a relationship, I can do a join or a subquery load on it in SQLAlchemy so that if I know I'm going to be traversing that relationship,
I don't end up with the dreaded N plus one performance problem where I thought I was doing one query and I'm doing 51 queries.
If I got a, you know, 51 or 50 results back, something like that.
Is that support flow through SQL model as well?
The joins?
Yeah.
So the thing is that SQL load actually just like exposes the same interface as SQLAlchemy because it's actually just using SQLAlchemy underneath.
And SQLAlchemy supports everything, including like lazy loading.
SQLAlchemy actually supports things that are not supported by many other ORMs.
I forgot the name.
Having primary keys that are composed of different, of several columns.
Things like that.
Composite indexes and composite keys.
Composite keys.
Yeah.
There's a bunch of things that SQLAlchemy supports.
If SQLAlchemy supports them, then the SQL model automatically supports them because SQL model is just generated directly from SQLAlchemy.
Yeah, that's really cool.
Now, the question I was thinking about is if I have a result from the database that has relationships, a relationship, and I return it from a FastAPI endpoint, is that going to go and start iterating the relationship?
Do I need to be worried about N plus one problems by returning these models that then are getting serialized in eager ways?
Yeah.
You know, where it's like tracing through all the relationships so it can build the whole JSON to get it back out.
And then return the whole database in a single JSON.
Wow, that took a while.
That was slow.
That would be so painful.
Yeah.
No, by default, FastAPI and SQL model won't include relationships in models, won't include them in the data that is returned back.
But if you need to include some of those, you can declare, again, using inheritance, you can declare a different model that defines, hey, this relationship, this particular attribute, this should be included.
That way you can define that particular one in the specific endpoints that you want to include the information in the resulting value.
Right, okay.
That will work to force SQL model and to force, well, FastAPI and SQL model to do all the N plus one queries and to just extract information and send it back.
But if you are returning that data including the relationships, you will probably want to eagerly load that information, which is something that is supported by SQL model and by SQL.
So you will load the information that you need including the relationships and then you just return that object directly and you define the model.
Hey, I want this to include the relationships.
So it will just like include the information that is already there.
I can see that this N plus one issue is without the join or eagerly load is happening through a profiler.
If I was doing this in something like Django or Pyramid, I could look into the debug toolbar and it'll actually show me the SQLAlchemy statements that are running.
It'll be like, why are there 50 queries on this page?
It's harder, I suspect, in FastAPI, especially if it's operating in API mode where it doesn't have like debug toolbars and stuff like that.
Probably one way you could see it is to say echo equals true on the engine.
Yeah, exactly.
Exactly.
Because FastAPI is not integrated with any database and SQL just makes it super easy to work with FastAPI, but SQL could be used with any other framework.
That was the intention.
SQL model doesn't depend on FastAPI and FastAPI doesn't depend on SQL.
So you just integrate very well.
But then you could just enable the table thing with SQLAlchemy that will then show all the particular SQL statements and show you, hey, this is what is running.
This is what is happening.
Right.
Yeah.
So if you're connecting to the database, then this is a SQLAlchemy thing, but obviously it'll flow through.
Right.
You just say when you create the engine, you give it the connection string, you can say echo equals true.
And if you are doing queries that are doing a bunch of indirect behind the scene, lazy queries for you, your console window, your terminal, whatever is just going to blow up with a query.
You're like, why is so much SQL screaming by, right?
Yeah, exactly.
Exactly.
That's what it will work.
Let's talk about editor support really quick.
So one of the things that's really nice about Pydantic is it requires you to state the types, whether those are fundamental types, whether those are nullable types, like optional of int, or they're nested types, like a user contains an address Pydantic model.
All of those scenarios result in really good editor support, right?
Yeah.
Yeah, exactly.
And what's the story for editors in SQL model?
That's something you specifically call out about how it has good support there.
So if you check the source code for SQL model, it's actually super short, but it's just like a bunch of a lot of tricks together.
And many of those tricks are actually about type annotations because the thing that allows your editor to provide the auto-completion and the inline errors are the declarations of these types, the type annotations or type hits.
And SQL model does a lot of internal work so that whenever you use any part of SQL model, you will get that type information in your editor.
For example, if you query some model, query some table to get information, to get data from the database, the result that you get back, the object that you get back, will have internally all that type information so that the editor will be able to provide you with all the autocoption and all the inline errors and all those things.
SQL model, in fact, sacrifices some of the more advanced or obscure or sophisticated use cases that SQLAlchemy supports and sacrifices those to instead get very good autocompletion and inline errors everywhere in the code.
And this is another thing that the results and clear in SQL model is that it uses some draft standards that are not even implemented yet.
I don't know, I don't know, I'm not part of the standards, official standards yet, but they are already supported by some editors.
For example, Visual Studio Code already supports providing autocompletion when you are creating a new instance of a particular class or for the particular class of a model.
Having this autocompletion is not very easy to do with other tools because the editor doesn't have any information about what are the parameters that you can pass, what are the arguments that you can pass?
Right.
When you create a Pydantic model, it doesn't in anywhere indicate here is the constructor or initializer and here are the keyword arguments that happen to be all the static values.
It will just say like keyword arguments or like data, star, star, something like that.
Yeah.
But I always think thanks for nothing when I see that.
Yeah, exactly.
But actually Pydantic 1.9 includes this same trick.
So now you get autocompletion in Visual Studio Code.
In PyChart, you already have autocompletion with Pydantic because they have a plugin for Pydantic to provide autocompletion for those things.
But it requires this particular plugin.
Now with this extension, you can get also autocompletion in VS Code.
And with the same extension without needing any plugin, you get autocompletion for SQL model in Visual Studio Code.
I think the people from PyChart were also taking out to maybe support the same standard, which will allow PyChart to provide automatic autocompletion for SQL model and other libraries like Pydantic and Attrs and others.
Yeah, that's great.
Definitely the widespread use of Pydantic effectively is forcing everyone to go like, all right, how can we make this work better on the creation side?
So RJL out there has a comment, which then leads me to an interesting question.
Says, I'm old fashioned.
I use direct SQL statements.
No ORM.
I really need to take the time to go down this route.
Indeed, I do think so.
It's certainly worthwhile.
What are your thoughts on using straight SQL versus not?
Then I'll ask my question.
So I think, you know, like it just, it's a lot about taste and how people prefer to code.
There's a lot of people that are so comfortable with SQL and that can do so many things with SQL very easily that it's just more efficient to just use SQL directly.
For me, some of the advantages with ORMs is that I get inline errors that I get autocompletion for what is the name of the attribute.
If I forget that I say secret underscore name, the editor will autocomplete that for me.
But if I'm typing that inside of just a long string in Python using SQL, then I have to remember because no one will tell me that I have a syntax error in my SQL or that I'm using an attribute that doesn't exist.
One of the things that actually blew my mind is PyCharm.
If you set it up to, if you basically connect the database to your project, it'll give you autocomplete and error checking inside strings inside Python.
That's super cool.
For your schema, which is amazing.
That said, I never do that because to me, one of the things that is super valuable, one is this autocomplete.
The other is refactoring as well.
Like, oh, did we change the name of that?
Well, oh, there was that one query we didn't update and now it crashes in production, but only sometimes when it hits this case.
And, you know, it's just the way it sticks together and stays consistent to me seems a lot stronger if you're using models as well.
Yeah, absolutely.
And also the ability to swap backends, right?
The way you do parameterized queries is different across different database backends.
Yeah.
Also, if you write SQL by hand, then you have to be super careful and probably you have to be a SQL wizard and, like, know how to sanitize all the data that you're putting or, like, you know, otherwise you could end up in nasty situations.
But, like, I'm saying that there's a lot of people that prefer, really prefer writing SQL directly.
You know, like, the same author of Pydantic, which SQL model is based on, prefers to write SQL directly.
And, like, he's using FastAPI and everything, but he's just more comfortable to me.
And the author of PyPy, PsychoPG, the driver for PostgreSQL, he just uses SQL directly.
It's just, like, more comfortable for him.
He uses FastAPI a lot, but still, like, it's just more comfortable.
So I guess, like, it depends a lot.
For me, I depend a lot on the tooling and editor support and refactoring, as you were saying.
Like, if I change a name, I know that it's changed everywhere because I won't remember.
I won't remember.
Well, did I make this mistake?
Yeah.
Yeah, absolutely.
Martin in the audience asked an interesting question.
Down here is a better example.
One of the challenges of ORMs is to make set-based operations apply back to the database.
Like, I want to change this field.
Like, I want to set a is on sale flag to true for all products where the price is less than $10.
Right?
Where I'm not going to pull.
I don't want to go, let me query all products whose price are less than $10.
Change it on the object and then push those changes.
I want to push the...
I just want to say, update where this set that.
You know what I mean?
Yeah, yeah.
What's the story about that with SQL model?
Because that's one of the things that can really just hammer productivity or speed, I guess,
is if you've got to, like, pull back a whole bunch of stuff to just make sort of consistent
changes across them.
You know what I'm saying?
Yeah, yeah.
Like, this is one of the use cases where you will want to interact directly with SQLAlchemy.
And you can do that through SQL model, but you can't write, like, where it's as complex
as you want through SQL model, but, like, just using pure SQLAlchemy underneath.
And, like, you can use, like, very advanced things with SQLAlchemy.
SQL model focuses a lot on, like, the simplest and most common use cases, providing, like,
the best developer experience.
You know, like, certainty that the code is as ever free as possible because you have all
these type annotations and all these type checks.
But for any case that is a little bit more advanced, you can just, like, drop down directly
to SQLAlchemy.
And because SQL model is just pure SQLAlchemy, the models are themselves.
Just SQLAlchemy.
So you can just, like, use SQLAlchemy directly.
In fact, you could use one of these models with a SQLAlchemy engine directly, and it would
work.
Okay.
Sky points out in the audience that you were too humble to call out the PR that got that
autocomplete for VS Code was actually by Tandic.
It was by you.
So well done.
Thank you.
Keep moving it forward on both fronts.
So what about performance?
There's extra goodness in the validation and the type conversions and stuff like that of,
say, Pydantic.
But is there a large overhead for using this, say, over SQLAlchemy over, say, raw SQL?
Yeah, well, so when you use, when you declare a model and you say, hey, this is table model,
so this is, like, the equivalent of a SQLAlchemy model, then a lot of the validation and that
stuff with Pydantic is a very written.
So when you create a model, it will not be validated on creation for that particular table
because this will be handled directly by SQLAlchemy.
And for example, with SQLAlchemy, you can create an instance of a model without setting
all the attributes and then you can set the attributes manually afterwards.
If Pydantic was doing validation for that, like, that will explode and that will say, hey,
this is invalid.
Yeah.
So when you are working with SQLAlchemy alone, well, like, with the SQL parts alone through
SQL model, then it's just, like, using SQLAlchemy.
Okay, so it's not really any different in terms of, like, whatever SQLAlchemy does, this
does in terms of performance.
Exactly, exactly.
It just has, like, and actually, the code is so, so sleek, it's so little that, like, whatever
is the overhead will be, I will think it will be negligible.
At the same time, I'm not optimizing for, like, squeezing the maximum performance, but
for getting the maximum correctness in the code and the best developer experience.
Because I feel it helps a lot more to be a lot, as a developer, building the tool and making
sure that it's all correct, than having the code super fast, but very difficult to debug and
to understand and to write correctly.
Okay, so I guess you just got to decide, like, is an ORM the right fit at all?
Yeah.
And if it is, then this is a pretty good choice if you like this API.
Yeah, absolutely.
If you were needing, like, the maximum performance that you can get, you will probably end up
just, like, getting an async, an async drive it directly and just, like, writing SQL directly
for the particular endpoint that needs this extra boosting performance.
But for most of the other cases, this will probably help making sure that the code is
correct and making sure that you can write quickly and, like, be done with the feature
that you're implementing quickly.
This portion of Talk By The Nome is brought to you by Tonic.ai.
Creating quality test data for developers is a complex, never-ending chore that eats in the
valuable engineering resources.
Random data doesn't do it.
And production data is not safe or legal for developers to use.
What if you could mimic your entire production database to create a realistic data set with
zero sensitive data?
Tonic.ai does exactly that.
With Tonic, you can generate fake data that looks, acts, and behaves like production data
because it's made from production data.
Using their universal data connectors and a flexible API, Tonic integrates seamlessly into
your existing pipelines and allows you to shape and size your data to scale, realism, and degree
of privacy that you need.
Their platform offers advanced subsetting, secure de-identification, and ML-driven data synthesis
to create targeted test data for all your pre-production environments.
Your newly mimicked data sets are safe to share with developers, QA, data scientists, and, heck,
even distributed teams around the world.
Shorten development cycles, eliminate the need for cumbersome data pipeline work, and mathematically
guarantee the privacy of your data with Tonic.ai.
Check out their service right now at talkpython.fm/tonic, or just click the link in your
podcast player's show notes.
Be sure to use our link, talkpython.fm/tonic, so they know you heard about them from
us.
I've got one more thing I want to talk to you about.
One of the things, though, that you spoke about is the ability of having, and this is just
generally true for SQLAlchemy.
These models, they're tied back to what's called a session or a unit of work to do with the database,
and you can't do a query, get a record, and then go from a separate situation and try to jam it back.
It's got to be stuck to the session that it comes from, right?
So you don't share the models across sessions, but one of the things that would be nice is just to have a single one.
And so FastAPI has a dependency injection system that you talked about can be used for basically always providing one and only one session to an API endpoint or a web endpoint that then could be the database management.
Like creating a unit of work that is the lifetime of the request, basically.
Want to talk about that?
Yeah, exactly.
I think you described it perfectly.
I don't know what else can I yell at the bullets, right?
So this dependency injection system is just like you declare some function and FastAPI will make sure to run that function and provide the value to all the things that need that value for one particular request.
Right.
This has nothing to do with the database, by the way.
This could be anything.
It could be a login framework, whatever, right?
Exactly.
So this is very useful for doing logging, for doing authentication, for doing authorization with roles and whatnot, for doing logging or setting up things that load stuff to remote servers like Sentry or like Datalog or I don't know, for all those things that you need to do.
And that can be, and that is, there is some logic that needs to be shared and that could run before the request is handled and maybe after the request is done.
And then you can share this information.
So in many frameworks, there's a concept of a middleware, which is something that runs before the request and after the request.
But then you have to run this thing for every request.
With dependencies, with this dependency injection system, you can define exactly where you want it to run.
And you can define, I want this to be run with a group of endpoints or with a group of path operations, as I used to call them, for just one particular endpoint or for a bunch of endpoints.
And with this system, you can extract and you can generate whatever it is that you need to generate for the particular request.
And the good thing about the dependency injection system is that if you're extracting information from the request, for example, from a header, then this information will be also extracted and included with past API and with all the open API and all these standards.
So you will get that information in the automatically generated user interface to explore the API.
Yeah.
Very cool.
So what steps do I have to take to do dependency injection to get that session to show up?
I remember you had it in the documentation, but I don't remember where it is right now.
I think for the particular case of SQL model, even though FastAPI and SQL model are independent, are made to be very compatible with that independent.
I have a lot of documentation about writing applications with FastAPI and SQL model in the SQL model docs.
The way that you will handle a FastAPI dependency in general is that from FastAPI, you import this special function depends.
And then you create some function that will return sole value.
This function will have the same style as any other function that handles a particular request.
So it can have some parameters with some types and that information will be extracted from the request.
And it just returns something.
This is just plain old function.
And then you pass function is this is what will be the dependence.
Then you pass that function as a parameter to depends.
And then you put depends as the default value of sole parameter in your main function that is handling the request.
I see.
So you could say session equals like depends.
Here's some function that will get called to create the session.
Session equals depends and calling depends with the function that is named get session or something like that.
Do you have a way to see both sides of that?
With dependency injection.
So does it just return the value or can you like create a session and then yield the value and then keep processing or something along those lines?
Exactly like that.
Okay.
You can create a session that you can yield the value.
And then after the request is done, you can continue doing more stuff after yielding that particular session.
So you can create a session, give the session from the dependency, and then the main code that will handle the request will have that session.
And then the same dependency can take care of closing the session after the request is done.
Exactly.
Try, yield the session, finally close it.
Maybe like if there's no exception, commit it, something like that.
Exactly.
Okay.
That's pretty flexible.
Yeah.
And in the main code that you have, you don't have to take care about creating the session or closing it or making sure that there are no exceptions because you can do all that stuff in the dependency and share that logic throughout your code.
And the other thing is that dependencies can themselves depend on other dependencies.
So you can create a dependency that gets the session for the database, and then you can use that in another dependency that gets the current user and extracts from the header or from the authentication token or whatever, extract the user ID, then gets the user ID from the database and then returns the current user.
So that you have a whole dependency that just takes care of returning the current user and making sure that it's authenticated.
And then in all your, and you can reuse that code in all your endpoints or in all the main functions that handle the requests.
And all those functions will be able to just get the current user right away without having to have all the logic to extract the information, process the token, all the stuff.
Sure.
This is very neat.
I haven't used this enough during my work with FastAPI, so I got to check this out.
All right.
Now, let's do a bit of a lightning round here of the Twitter questions, because I've seen some of these questions come up in the live chat, but I also think I pulled out these ones that I thought people put on Twitter that were pretty good.
LeMatte Webster says, any plans to ramp or replace Alembic to make migrations more developer friendly?
So first of all, migrations, you've got to keep your database in sync with your models.
Otherwise, SQLAlchemy and hence SQL Model will freak out about that because it's going to be a problem.
But Alembic is, while it works, is a little bit hard to say, like, here's all the models you need to pay attention to.
And here's the scenario where you run.
It's a little bit clunky.
It works well, but it's not super smooth.
And I think that's what Matt's asking here.
Alembic is the official tool from SQLAlchemy to do the migrations.
And because SQL Model is itself also just SQLAlchemy underneath, Alembic works with it perfectly.
Alembic is a great tool.
It's super advanced and helps a lot.
It can even generate automatic migrations and things like that.
I think the main problem with Alembic is that in some cases it's not as intuitive.
So, yes, what I want to do at some point is to wrap a bit of Alembic.
I will replace it because it's already doing a magnificent job.
But, and it will be super difficult to write all that logic and all that work that Mike has been doing for a very long time with SQL Model and Alembic.
I will wrap it and I will try to add a bit more of documentation to explain how to handle the simplest cases, which is the same that I'm doing with SQL Model.
If you need something more confidence, that will probably just go Alembic directly or to SQLAlchemy directly.
Related to this, not Twitter question, Michael question, do you have any thoughts about testing code using these models and stuff like fake data or mocking out the database beyond just the standard stuff you would do to SQLAlchemy?
Like, is there anything special about SQL Model that makes testing it easier or different than SQLAlchemy?
No, the testing will be pretty similar to SQLAlchemy.
Pretty similar.
Yes, I just have like a bunch of documentation of how to do testing and even how to do testing with FastAPI applications using SQLAlchemy and how to, for example, use SQL Lite database or testing that will be run on memory instead of the production database that will be a PostgreSQL or MySQL or whatever.
Just change the connection string to the engine and let it go.
Yeah, okay.
And then make it run with a database in memory and then make it work correctly with threads and everything.
So something like a pytest fixture that initializes the database, but you just use colon memory colon for the connection string so that it just goes away.
Yes, exactly like that.
It's documented exactly like that.
All right, right on.
All right.
Ricky Lim says, would SQL Model be part of, should, could be part of the standard Python library?
I have some thoughts on this, but I want to hear your thoughts first.
I have some historical perspective on this, but I want to hear your thoughts on this.
Okay, so it will not be part of the standard library.
Ideally, it will not be part of the standard library because if it was part of the standard library, it will mean that it will be available in Python 3.13 or something.
And the users of Python 3.10 right now will not be able to use it on one side.
On the other side, having more stuff in the Python standard library adds more inconvenience and more burden to the core maintainers, to the core developers, Python, which makes it even more difficult for them to continue supporting Python and all the different versions.
And it will also complicate things for Red Cannon that is trying to figure out a way to slim down Python so that you can, for example, run it directly on the web browser.
Right.
How do we build it in WebAssembly?
This doesn't help.
Yes, WebAssembly.
And you know, Red Cannon and Kristen Hanks have been doing like a very cool job.
It's very exciting.
A lot of that will probably require actually is leaning down a bit.
Standard library.
As far as I understand, but I'm not no expert.
Yeah, I imagine a world where we have, I don't know what the right word for it is, but there's like a standard cross environment Python minimum set of language features of standard library where things like, you know, stuff that talks on the network or does UI things or whatever.
We're just, that is not part of this like minimum subset of Python that we're guaranteed to have so that we can put it on WebAssembly.
We can put it on mobile devices.
We can put it on servers.
And as long as you program to this minimum set, your place where your Python can exist is broader, you know, like MicroPython potentially.
I think that that's the trend and not the trend towards putting more stuff there.
Yeah, exactly.
Absolutely.
And it's fun that it's already happening.
MicroPython is already that.
It's just that it cannot say it's a standard Python because it has to link that a bunch of things.
But being able to have a microcontroller and write code in Python that is running the microcontroller, that's amazing.
That's mind blowing.
Yeah, the most mind blowing thing for me is that you can hook a Lambda expression directly to a hardware interrupt.
That is like, what?
You can do what?
That's amazing.
The historical perspective that I want to bring up here is I believe the core developers actually considered this for requests.
And they decided that, no, they're not going to put requests in the standard library to replace URL lib because it would limit requests' ability to grow.
Like, changes could only come once a year.
It couldn't come three times a week if there was important changes, right?
Like, the speed of development would be hindered.
So they said, you know what?
No, we don't want to.
Yeah.
Plains to tell us.
Yep.
All right.
Next, Dimitri Figo says, are you considering working on generating TypeScript declaration files based on what's defined on the FastAPI backend?
That was that documentation I showed where it has the schema and all that and, like, the endpoints.
Yeah.
To make it, to explain it a little more, when you go to the automatic interactive documentation for the API, that is all based on this standard schema of the API called OpenABI.
This is just a huge JSON that defines all the data shapes that you're using, all the endpoints, everything.
But that same thing, because it's a standard, then you can use that same thing to generate code for clients that communicate with your backend.
And in fact, there's a bunch of client generators for many languages, including TypeScript.
And I have used them.
I have used some of them.
And they are actually very, very good.
Like, you can achieve things like defining in the backend where the data shapes that you're using.
Then you update something.
And then you regenerate the client in the frontend.
And now, after that, the frontend team will be able to have access to this new API endpoint with autocompletion in their editor and everything.
It works very well.
It's super exciting.
I just haven't had the time to document, like, the whole recipe to make it work.
But it's already there.
It's already working.
And it already does a great job.
Yeah.
Maybe somebody wanted to contribute a PR or do some help there.
They could, right?
Yeah.
You know, like, even a blog post will just, like, that will be a lot faster to get out.
And that will help a lot.
And a lot of people.
Indeed.
Zach Kud says, I'd love to hear how you approach figuring out the integration with SQLAlchemy.
I mean, we talked a bit about this.
But any other lessons you've learned from basically getting in the middle of SQLAlchemy and all of that it does?
Yeah.
It's very interesting that SQLAlchemy was created at a time where, like, it was, I don't know, Python 2.something.
There were no context managers.
So that thing that you do with a blog that you say with something, something, as blah, blah, blah.
And then inside of that report code, that was not available, but that didn't exist.
SQLAlchemy was made before that.
So SQLAlchemy had to do a lot of sophisticated tricks to make everything work.
And then getting down inside of it and trying to understand a lot of things like, why is this thing doing this?
I'm working like this.
And it's because of those things.
I think I ended up learning a lot about those little details and a lot about how classes work internally and how a class is an instance of what and all those things and how you can configure all that.
But the idea with SQL World is to make it super easy for you to use it without having to deal with all the internal complexity.
Because it's crazy.
You don't have to know.
Only you had to know and go through it.
So I think it's worth pointing out, I did have Mike Bayer on the show recently to talk about SQLAlchemy 2.0 and how they're moving to have basically the client side view of that be everything as a context manager and sort of change that up a bit.
So how close is this to the 2.0 model or is it the 1.0 model API?
Yeah.
So Mike Bayer did a lot of work to make the compatibility transition as easy as possible.
And SQLAlchemy, the latest available version, which is 1.4, is compatible with the previous style and with the new style.
So code that is written with the new style will be compatible with SQLAlchemy 2.0 whatever and above.
SQL model is based on this new style.
So if you, for example, if you have an old application with SQLAlchemy, the first thing that you will want to do is to migrate to SQLAlchemy 1.4 and make sure that it's compatible with the new style.
And make sure that you don't have any warnings.
That's the main thing that you will do to make sure that it's compatible.
And then after that, you can migrate to SQL more.
The migration is also super simple.
It's just like changing some classes for type annotations.
Yeah.
Absolutely.
There was a question.
Yeah.
So the question by Python at night was, what would the level of effort or benefit, if any, of converting SQLAlchemy models and schemas to SQL model?
Sounds like the effort is small and the benefit is all the features we spoke about, right?
Yeah.
Okay.
Would you recommend it?
I mean, people are using SQLAlchemy.
They're like, you know, I'd really would like to have some of that Pydantic magic.
When would you say, okay, the trouble of making a change is sufficient?
So if it was me, I would just use it right away.
Right now, it's in version 0.0 point something.
I will release 0.1 point 0 once I have 100% of test coverage.
Right now it's 97% just because I'm not freaked out of that.
But like most of it should already work.
It's actually very simple.
And if anything, the work that it does is actually very small because all their needs is all done by SQLAlchemy.
If anything went wrong, you could also just like switch back to SQLAlchemy directly.
It's just that you will lose the benefits.
Right.
Basically change your class back to driving from SQLAlchemy based and you're good to go.
Yeah.
Okay.
The benefit that you will get is auto-completion and inline errors everywhere where you are using these classes that you will normally not get.
And the integration with response model.
And the integration, yes, of course, integration with response model is that, yeah, actually that's a lot of code that you will save if you can share that with Bidantic and with SQL Mode.
Just make sure that you follow all the information about how to pin and how to upgrade versions because it's very detailed how you should go about that.
Because as things are still changing and as it still has like a little bit of extra testing to do, you should be careful about how to pin.
Just not install like whatever version comes.
Just make sure that you've been the right version.
You have tests and then when you upgrade, you make sure that the tests are pausing and then you can upgrade the version.
Yeah.
Good advice.
Already talked about that one.
So Lado asks, are you going to stretch modern Python conventions from the backend part, which you already did?
We talked about like using the types and stuff for model binding to Python challenging front end as well.
Should we expect something like Reactipy, like React, but in Python?
That will be it.
Do you care about the front end in the sense that you have any intention to build stuff for it?
Yes, I care a lot from then and I have actually, I think I started in that and I have worked with Angular, React, Vue.js and like all the stuff.
I think it will be amazing to be able to write Python in, to write Python for front end.
But if someone is going to make that happen at some point, they'll probably let Connor and Christine Haynes making WebOssently work for Python.
Yes.
Yeah.
You need the runtime there first and then it'll go much more easily.
I absolutely think that it is not quite negligence, but it's close that the browser makers don't package other runtimes that are WebAssembly compatible, right?
Like they should go to Ruby, they should go to Java, they should go to .NET and they should go to Python and say, are you willing to provide us a runtime that does X, Y, and Z that we can integrate in a generic way that we can include in our browser?
So you don't have to say, oh, well, you can't use these other advanced things because the WebAssembly download is 10 megs.
Well, if Firefox, Chrome, and Safari all shipped, you know, five of those, the five most common languages as binaries, like you would just have it.
And it would just be this WebAssembly.
Why does this not happen?
So, but that's the real problem to Reactipi is that that's not there, right?
Yeah.
All right.
Quasi says, what is he doing to address the bus factor?
That is, if you get hit by a bus and then related to that in the audience, Prashat Rana says, well, you include a moderator to the project so it can become a community driven project and there's less burden on you.
I think those are kind of similar questions from a different perspective.
Yeah.
And so the thing is for most of these projects, like most of the work can already be done a lot by the community.
It's not that the work is, cannot be done.
I just want to simply enable a bunch of permissions to a lot of people.
We just go and merge all requests that very quickly because I like to make sure that everything works.
For example, yesterday, for yesterday's FastAPI release, it had like four approvals, the pull request, but still it had a couple of bugs and a couple of things that needed to be solved.
And like, I need to make sure that the code quality is still kept and that everything is working correctly.
So for now, I'm still like making sure that I can give each one of the pull requests.
But if people went and checked those pull requests and reviewed the code and tested it and like made sure, hey, this is working and I'm using it and it's working in my application or things like that, that will, of course, help a lot.
Of course, that will help a lot.
That's great.
Obviously, it's open source.
People can fork it.
They can run with it.
Like if you actually got hit by a bus, I think FastAPI would keep going.
There'll just be a figuring out of like, all right, well, where's it going to send her back around before it settles down?
Yeah, exactly.
Not that anybody wants to.
I think these questions are a reflection of how significant the impact you're having on the community is, right?
Yeah.
And, you know, like it's, I find the bus factor fun.
I have been wanting to write a blog post about that for a while because I think the bus factor is something that works a lot for investors or for like founders that are not developers.
And they are like associating with someone that is the only one that knows the product, but they want to be the owners of half of it.
And if this person dies, they just lose all their investment.
But when you're working with one of the things.
They can keep it going.
Yeah.
Absolutely.
Like, for example, many of the projects from Nicole is mainly Tom Christie, which is one person.
The maintainer of Flask, which is huge.
It's mainly David Lord, and he's just like suffering through all of it and through all the abuse of developers and doing like a lot of the work.
Probably like, I don't know, see, he has like another contributor or something like that.
In the case of FastAPI, there's people like Marcelo that is helping a lot.
And even Samuel Copping is also helping.
That, you know, helps a lot with keeping the community, maintaining it, and like doing all the work that is needed to be done underneath.
That doesn't really affect how it's working, you know?
Like, the fact that the repository is not in another GitHub owner, in a GitHub organization or something like that.
It's just because it's easier to handle.
But at the same time, there's a lot of people that are already contributing.
And that's the work that actually makes maintaining it and sustaining it.
Exactly.
That's the stuff that matters.
Yeah.
Absolutely.
Alupia, just by the way, out there also says, I just want to say thanks to...
You, so, your work is so important and just great.
So, yeah, definitely a lot of people out there loving it.
Okay, let's keep going.
Back to SQL model.
Roadmap for the future.
What are the plans?
So, migrations.
I want to have a small wrapper to have a command line interface built on top of typer so that you can get into completion in the terminal as well.
To have migrations based on an embed.
To documentation for using ASIC with SQL model.
ASIC is already supported by SQLAlchemy.
Yeah.
And that's a new thing, right?
That is, you've got to create now an ASIC client or an ASIC session rather.
So, I think it is instead of a regular session in SQLAlchemy.
But that's one of the 2.0 big changes that Mike just pushed out.
So, that flows through?
Yeah.
Like, you can already use it.
In fact, there's people using it in applications, in production applications right now.
But it's just that I don't have it documented yet.
SQLAlchemy already supports both the normal, the blocking interface, the regular interface, and the ASIC interface.
And you can already use it with SQL models.
But I want to document all that.
Right.
So, what you should do is you should just change your dependency injection based on whether you have a DEF method or an ASIC DEF method in FastAPI.
And either create an ASIC client or ASIC session rather, or regular session, and then boom, off you go, right?
That is one of the things that I think is so smart about the design of SQLAlchemy, that SQL model in Arids, is that the thing that handles if it's ASIC or not is the engine, not the models themselves.
So, you can use the same models even if it's ASIC or not.
I agree.
Very nice.
Good question by Lars, but I'm going to keep going because we're short.
David Smith asked, are there plans to add ASIC?
Yes.
I think the question is, are there plans to document ASIC?
Yeah, exactly.
At this point, right?
We kind of touched on this one about whether you should use it for an existing, like, should you be migrating?
And we touched on that one.
Rainer, who I think I saw in the audience, yeah, earlier.
Hey, Brandon.
Asked, could you go ahead and make NoSQL model?
This is a chance for me to mention Beanie out there.
I wanted to kind of ask you if you'd had a chance to look at this.
So, Roman Wright also was out in the audience.
I saw them.
So, Beanie is an ODM, like for MongoDB, but also basically very similar based on Pydantic.
So, I thought it was a, this immediately came to mind when I thought about SQL model is like, well, here's the MongoDB version.
Yeah.
It also, you know, tries to do the same thing.
Have you thought about a NoSQL story?
Have you looked at Beanie?
What are your thoughts here?
Yeah.
And like, I really like both, that there are two alternatives for MongoDB with Bynantic.
One is Beanie.
The other one is Odmantic.
I think they are both doing a great job.
I like this particular thing about Odmantic is that it uses the same style of the interface of SQLAlchemy.
that the thing that decides if it's async or not is the engine and not the model, which means that it will probably be easier to implement.
Because both Beanie and Odmantic, both are async.
Yeah.
Both having the engine being the one that is async or not will allow implementing a regular or blocking version of it.
So that you could have MongoDB models that are shared and reused for async code and for blocking code in the same application.
So you could migrate more slowly and things like that.
But I think both are doing a great job and like have a very nice interface that is like very close to Python.
Yeah.
Just as a sidebar, I do wish it was easier in Python to convert an async call to a synchronous call, knowing that it would block.
Just go like, okay, here's async method.
If I could just go dot wait or dot result or something and just make it execute and basically stop the async running from there.
Then you could just do the query like dot.
Yeah.
Give me the answer, right?
Whereas it's a little more tricky.
You've got to get it like in the loop and run the loop to completion and stuff like that.
Yeah.
But you know, like that's why I just built a async, which is built on top of any IO.
And the idea is that you have this function asyncify and the function syncify just to do that.
So you can pass one function that is async and it will be run inside of the main event loop in async way.
Or you can say, hey, asyncify this thing.
And it will run the blocking function in a thread pool so that it's not blocking the main event loop, all the stuff.
But it's actually, the work is actually all done by any IO.
It's again, just doing the same thing of getting type annotations and other completion and all the stuff on top of the thing that is doing the work.
Yeah, I definitely came across this not long ago and it looks very exciting.
And I wanted to talk to you about it.
But as you can see, we're way over time already for just our main thing.
So let's go back to it.
But maybe next time you're on, we could just talk about async stuff all day.
Mike out there asks, what is the risk of using it in production?
The risk is the risk that you have for using any software in production.
Let me maybe rephrase it.
So what is the readiness for production, I guess, is probably what he was thinking.
Yeah.
So the thing is, most of the work is done by Pylandtica and SQLAlchemy.
And they have been used for years and they are doing an amazing job and they are already used by a lot of tools.
SQLModal only does like a little bit of extra stuff on top just so that you can need all the type annotations.
Most of the work is done by those.
The other thing is that the test coverage is at 97%.
So you have some certainty that it's working as intended, at least.
I want to have it at 100% and I want to have tests in continuous integration with several databases.
Because right now the tests are only run in SQLite.
But you know that all the SQL stuff is actually done by SQLAlchemy, which is already tested in all the databases.
So, you know, like there's a trade-off of trying this thing that still has a little bit of extra stuff to do.
Most of the extra things that I will do on top of SQLModal are actually documentation.
Not that much whole changes.
And the ability to flip from one to the other pretty quickly means if you had to say, oh, no, this is not working out.
We're switching back.
It's not like, oh, well, you're completely rewriting.
Yeah.
It's not that much work.
You just have to make sure that you're in the versions and you upgrade correctly.
But otherwise it should be.
And you will get more certainty about the code correctness because you have all the type.
So, yeah.
Yep.
All right.
We got one question left.
You have this ability to take what are often somewhat existing APIs and then improve them in ways that people really connect with.
Right.
Like FastAPI didn't start from open TCP socket and let's start from there.
It started on top of Starlet, right?
Like you already mentioned, Tom Christie.
This is on top of two very important libraries that they should go together but didn't.
So the question Pierre asks is, you know, how did you learn to sort of come up with APIs like you have?
Like what's your recipe for building these?
I think the thing is that I have been always trying to solve a problem and I have always trying to improve my developer experience.
and to improve the way that things work for me.
And like I have ended up just like trying to understand what is the best way to achieve those things.
And at some point I ended up learning a little bit about type annotations and I realized that, hey, this can be super powerful.
I can reuse it for different things.
And after looking and looking for different frameworks that did what I wanted, I ended up like saying, okay, I just have to build this because it doesn't exist yet.
But it was just like trying to achieve getting the thing that I want.
For example, I wouldn't go and build a NoSQL model or MongoDB because there's already Beanie and Nolpatic.
They are already solving the problem.
I try to avoid building new things.
But when there's the case that nothing is really something that I want to have, then I go and try to build it.
The other thing is I think one of the main features of all these tools is just a good documentation.
And I guess like the only thing about that is that I write it as I will have liked to learn those things when I was just starting and was struggling to understand what are all these things.
And I always have in mind like how will that newbie learn these things and understand it.
I guess that's probably the main thing that I'm trying to do.
To make it as easy to use as possible.
Yeah, I feel there's a strong blend of like, let's take the new things that are really useful that maybe not everyone's using and make them very accessible.
Make them very easy and default and so on.
Yeah.
Yeah, that's the learning spirit, I think.
Yeah, absolutely.
All right.
Well, that's all the questions.
We've been going a little bit long, but I really appreciate the time.
Let's just real quickly ask you the final two questions.
I'll let you get out of here.
All right.
So if you're going to write some Python code, work on SQL model or something else, what editor do you use these days?
I think the both main editors, Visual Studio Code and PyCharm are doing an amazing job at supporting all these tools.
Right now, my main one is Visual Studio Code, but yeah, I will just one of those.
Okay.
Very good.
And then notable PyPI packages.
I feel like we touched on these a little bit.
We just mentioned them both, Manthic and Beanie for doing the same SQL model stuff, but for MongoDB.
Yeah, right on.
I agree.
Those are both great.
All right, Sebastian, it was great to have you back and congratulations on SQL model.
Maybe next time we'll talk async.
What do you think?
Awesome.
Sounds great.
Thank you very much, Michael, for inviting me.
Thank you everyone for staying this long.
Yeah, you bet.
See ya.
Bye.
This has been another episode of Talk Python to Me.
Thank you to our sponsors.
Be sure to check out what they're offering.
It really helps support the show.
Datadog gives you visibility into the whole system running your code.
Visit talkpython.fm/datadog and see what you've been missing.
Go throw in a free t-shirt with your free trial.
Tonic.ai creates quality test data that does not contain personally identifiable information.
Your generated data sets are safe to share with developers, UA, and data scientists.
Most importantly, they behave like production because they're made from production data.
Check them out at talkpython.fm/tonic.
Want to level up your Python?
We have one of the largest catalogs of Python video courses over at Talk Python.
Our content ranges from true beginners to deeply advanced topics like memory and async.
And best of all, there's not a subscription in sight.
Check it out for yourself at training.talkpython.fm.
Be sure to subscribe to the show.
Open your favorite podcast app and search for Python.
We should be right at the top.
You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the direct RSS feed at /rss on talkpython.fm.
We're live streaming most of our recordings these days.
If you want to be part of the show and have your comments featured on the air, be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
This is your host, Michael Kennedy.
Thanks so much for listening.
I really appreciate it.
Now get out there and write some Python code.
I really appreciate it.
Thank you.