Communicating with a SQL database with Tortoise ORM
When dealing with relational databases, you might wish to abstract away the SQL concepts and only deal with proper objects from the programming language. That's the main motivation behind ORM tools. In this section, we'll examine how to work with Tortoise ORM, which is a modern and asynchronous ORM that fits nicely within a FastAPI project. It's greatly inspired by the Django ORM; so, if you've ever worked with it, you'll probably be on familiar ground.
As usual, the first step is to install the library using the following command:
$ pip install tortoise-orm
If you need drivers for database engines such as PostgreSQL or MySQL, you can install them, as explained in the documentation at https://tortoise-orm.readthedocs.io/en/latest/getting_started.html#installation. We're now ready to work!
Creating database models
The first step is to create the Tortoise model for your entity. This is a...