Piccolo ORM

1.33.0 · active · verified Thu Apr 16

Piccolo is a fast, user-friendly, and fully type-annotated ORM and query builder for Python, primarily focused on asynchronous operations. It supports PostgreSQL, SQLite, and CockroachDB, and comes with batteries included like migrations, an admin GUI, and ASGI application templates. Currently at version 1.33.0, Piccolo releases frequently, often with minor version bumps every few weeks, incorporating new features and improvements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates defining a simple Piccolo Table, initializing an in-memory SQLite database, performing basic CRUD (Create, Read, Update) operations, and running the async code.

import asyncio
from piccolo.table import Table
from piccolo.columns import Varchar, Integer
from piccolo.engine.sqlite import SQLiteEngine

# 1. Define your database engine (in-memory SQLite for quick start)
DB = SQLiteEngine(path=':memory:')

# 2. Define your Table
class Band(Table, db=DB):
    name = Varchar(length=100)
    popularity = Integer(default=0)

async def main():
    # 3. Create tables
    await Band.create_table(if_not_exists=True)

    # 4. Insert data
    await Band.insert(
        Band(name="Pythonistas", popularity=1000),
        Band(name="Asyncio Allstars", popularity=800)
    ).run()

    # 5. Select data
    all_bands = await Band.select(Band.name, Band.popularity).run()
    print("All bands:", all_bands)

    popular_bands = await Band.select(Band.name).where(Band.popularity > 900).run()
    print("Popular bands:", popular_bands)

    # 6. Update data
    await Band.update({"popularity": 1100}).where(Band.name == "Pythonistas").run()
    updated_bands = await Band.select(Band.name, Band.popularity).run()
    print("Updated bands:", updated_bands)

    # 7. Close the connection pool (important for persistent databases)
    await DB.close_connection_pool()

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →