Workshop

No More Raw SQL: SQLAlchemy, ORMs & asyncio

Friday, May 24

14:45 - 16:50
RoomMozzarella
LanguageEnglish
Audience levelIntermediate
Seats left1
    Elevator pitch

    Syncing service data with your DB can be tricky. In this workshop, you’ll learn how to use SQLAlchemy, a powerful SQL toolkit that simplifies this task. We’ll cover how to leverage SQLAlchemy’s Object Relational Mapper system, and how to use SQLAlchemy’s asyncio extension for async services!

    Abstract

    This tutorial is for Python developers of any level who write applications that interact with databases and want to learn how to leverage a tool like SQLAlchemy to seamlessly interact with a database and manage data in a Pythonic way.

    You’ll walk out of this interactive tutorial having learned how to:

    • Use SQLAlchemy for database operations in Python, enhancing the readability and maintainability of the code
    • Build Python classes (ORMs) that represent the database tables
    • Experiment with different relationship-loading techniques to improve querying performance
    • Utilize SQLAlchemy’s asyncio extension to interact with databases asynchronously

    To give more detail, we’ll be covering the following:

    • Introduction
      • What is SQLAlchemy?
      • Why use SQLAlchemy and advantages?
      • Components Overview such as engine, dialect, connection pool, etc.
    • Initial setup for the hands-on workshop with GitHub Codespaces
      • Run and explore example service that has database queries with raw SQL
    • Adding SQLAlchemy to the example service
      • Set up SQLAlchemy
        • Set up engine & dialect to connect with the DB
        • Use SQLAlchemy Core to query the DB
      • Add ORMs
        • What are ORMs?
        • How to represent a basic table?
        • Modeling different relationships (e.g., 1-1 and 1-many) between the classes
        • Using ORMs to query the DB
      • Convert other queries using SQLAlchemy
    • Improve performance by changing relationship loading techniques
      • Consequences of certain models: Talk about N+1 problem and bidirectional relationships
      • Discuss different loading techniques, such as lazy loading and eager loading
    • The SQLAlchemy.asyncio extension
      • Brief description of asyncio
        • What are coroutines?
        • Scheduling tasks on the asyncio event loop
      • A hands-on walkthrough of SQLAlchemy’s asyncio extension
        • Setting up SQLAlchemy in async mode
        • Performing a query and inserting it into the database
        • Using ORMs in queries using asyncio
    TagsAsyncIO, SQL Alchemy, Distributed Systems, Databases
    Participant

    Aya Elsayed

    Aya Elsayed is a software engineer at Bloomberg. She’s a leader in the company’s Python Guild, which aims to support Python engineers at Bloomberg to innovate, develop Python packages, and stay connected to the broader Python community. Aya previously spoke at a few conferences, including PyCon US 2023, PyCon Italia 2023, and PyCon UK 2022, as well as delivered workshops at internal and local meetups like PyLadies London. She enjoys Pilates, hiking, and trying out restaurants around London.

    Participant

    Rhythm Patel

    Rhythm Patel is a software engineer at Bloomberg. He is a part of Bloomberg’s Python Guild, which is dedicated to aiding Python engineers, fostering innovation, creating and maintaining Python packages, as well as acting as a bridge to the wider Python community. Rhythm has spoken at PyCon Germany 2024, PyCon US 2024, PyCon UK 2023, and other internal conferences. When he’s not working, you can find him playing football or tennis, traveling and hiking, or volunteering at London’s Royal Parks and London Zoo.