Top Related Projects
The Database Toolkit for Python
An async ORM. 🗃
Pony Object Relational Mapper
Familiar asyncio ORM for python, built with relations in mind
a small, expressive orm -- supports postgresql, mysql, sqlite and cockroachdb
The Web framework for perfectionists with deadlines.
Quick Overview
Piccolo is an async ORM and query builder for Python, designed to work seamlessly with PostgreSQL. It offers a simple and intuitive API for database operations, supports migrations, and provides a powerful admin interface for managing data.
Pros
- Asynchronous by default, allowing for efficient database operations
- Intuitive query syntax that closely resembles Python code
- Built-in support for database migrations and schema management
- Includes a customizable admin interface for easy data management
Cons
- Primarily focused on PostgreSQL, with limited support for other databases
- Relatively new compared to more established ORMs like SQLAlchemy
- Learning curve for developers not familiar with async programming
- Documentation could be more comprehensive for advanced use cases
Code Examples
- Defining a table:
from piccolo.table import Table
from piccolo.columns import Varchar, Integer
class User(Table):
name = Varchar()
age = Integer()
- Performing a query:
# Fetch all users over 18
users = await User.select().where(User.age > 18)
# Print user names
for user in users:
print(user.name)
- Inserting data:
# Create a new user
new_user = User(name="Alice", age=25)
await new_user.save()
- Updating data:
# Update all users' age by 1
await User.update({User.age: User.age + 1}).run()
Getting Started
- Install Piccolo:
pip install piccolo
- Create a database configuration:
# config.py
from piccolo.engine.postgres import PostgresEngine
DB = PostgresEngine(
config={
"database": "my_db",
"user": "postgres",
"password": "password",
"host": "localhost",
"port": 5432,
}
)
- Define your tables and run migrations:
# tables.py
from piccolo.table import Table
from piccolo.columns import Varchar, Integer
class User(Table):
name = Varchar()
age = Integer()
# Run in terminal:
# piccolo migrations new
# piccolo migrations run
- Use Piccolo in your application:
from tables import User
async def main():
await User.create_table().run()
new_user = User(name="Bob", age=30)
await new_user.save()
users = await User.select().where(User.age > 25)
for user in users:
print(f"{user.name} is {user.age} years old")
# Run the async function
import asyncio
asyncio.run(main())
Competitor Comparisons
The Database Toolkit for Python
Pros of SQLAlchemy
- More mature and widely adopted, with extensive documentation and community support
- Supports a broader range of databases and offers more advanced features
- Provides both ORM and Core functionality, allowing for more flexibility in usage
Cons of SQLAlchemy
- Steeper learning curve due to its complexity and extensive feature set
- Can be slower for simple queries compared to lighter-weight ORMs
- Configuration and setup can be more time-consuming
Code Comparison
SQLAlchemy:
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
Piccolo:
from piccolo.columns import Integer, Varchar
from piccolo.table import Table
class User(Table):
id = Integer(primary_key=True)
name = Varchar()
SQLAlchemy offers a more verbose syntax but provides greater flexibility, while Piccolo aims for simplicity and readability. SQLAlchemy's approach allows for more complex configurations, whereas Piccolo focuses on a straightforward table definition. Both ORMs achieve similar results, but SQLAlchemy's syntax reflects its broader feature set and adaptability to various use cases.
An async ORM. 🗃
Pros of ORM
- Built on top of SQLAlchemy, leveraging its powerful features and wide database support
- Designed for asynchronous operations, making it well-suited for modern async frameworks
- Supports type annotations, enhancing code readability and IDE integration
Cons of ORM
- Less mature project with potentially fewer community resources and extensions
- May have a steeper learning curve for developers not familiar with SQLAlchemy concepts
- Limited documentation compared to more established ORMs
Code Comparison
Piccolo example:
class User(Table):
name = Varchar()
age = Integer()
user = User(name="John", age=30)
await user.save()
ORM example:
class User(Model):
name: str
age: int
user = User(name="John", age=30)
await user.save()
Both ORMs offer similar syntax for defining models and performing basic operations. ORM's use of type annotations is more explicit, while Piccolo's approach is more reminiscent of traditional ORMs. The async nature of both libraries is evident in their await
usage for database operations.
Pony Object Relational Mapper
Pros of Pony
- More mature project with a larger community and ecosystem
- Supports advanced ORM features like lazy loading and composite keys
- Offers a unique query language (PonyORM) for more expressive and readable queries
Cons of Pony
- Steeper learning curve due to its unique syntax and concepts
- Less flexible for raw SQL queries and database-specific optimizations
- Slower development and release cycle compared to Piccolo
Code Comparison
Pony:
from pony.orm import *
db = Database()
class Person(db.Entity):
name = Required(str)
age = Required(int)
db.bind(provider='sqlite', filename='database.sqlite', create_db=True)
db.generate_mapping(create_tables=True)
Piccolo:
from piccolo.table import Table
from piccolo.columns import Varchar, Integer
class Person(Table):
name = Varchar()
age = Integer()
Person.create_table().run_sync()
Both ORMs provide a way to define models and create database tables, but Pony uses a more declarative approach with its Entity
class, while Piccolo uses a more straightforward table-based structure. Pony requires explicit database binding and mapping generation, whereas Piccolo's setup is more concise.
Familiar asyncio ORM for python, built with relations in mind
Pros of Tortoise-ORM
- Supports asynchronous database operations, making it well-suited for high-performance applications
- Provides a more extensive set of built-in field types and validation options
- Offers a robust query builder with support for complex queries and joins
Cons of Tortoise-ORM
- Steeper learning curve due to its more complex API and asynchronous nature
- Limited support for raw SQL queries compared to Piccolo
- Requires more boilerplate code for model definitions and database setup
Code Comparison
Tortoise-ORM model definition:
from tortoise import fields, models
class User(models.Model):
id = fields.IntField(pk=True)
name = fields.CharField(max_length=50)
email = fields.CharField(max_length=100)
Piccolo model definition:
from piccolo.columns import Varchar, Integer
from piccolo.table import Table
class User(Table):
id = Integer(primary_key=True)
name = Varchar(length=50)
email = Varchar(length=100)
Both ORMs offer similar functionality for defining models, but Piccolo's syntax is more concise. Tortoise-ORM provides more built-in field types and validation options, while Piccolo focuses on simplicity and ease of use. The choice between the two depends on project requirements, performance needs, and developer preferences.
a small, expressive orm -- supports postgresql, mysql, sqlite and cockroachdb
Pros of Peewee
- More mature and established project with a larger community and ecosystem
- Supports a wider range of databases, including SQLite, MySQL, and PostgreSQL
- Lightweight and simple to use, with a clean and intuitive API
Cons of Peewee
- Lacks some advanced features like async support and schema migrations
- Less focus on type hinting and modern Python features
- May require more manual configuration for complex queries and relationships
Code Comparison
Peewee:
from peewee import *
db = SqliteDatabase('people.db')
class Person(Model):
name = CharField()
birthday = DateField()
class Meta:
database = db
Piccolo:
from piccolo.table import Table
from piccolo.columns import Varchar, Date
class Person(Table):
name = Varchar()
birthday = Date()
Both Peewee and Piccolo are Python ORMs, but they have different approaches to database interaction. Peewee focuses on simplicity and ease of use, while Piccolo emphasizes modern Python features and async support. Peewee has a larger community and more extensive documentation, making it a solid choice for many projects. However, Piccolo's async capabilities and type hinting support make it attractive for developers working with modern Python applications, especially those using async frameworks like FastAPI or ASGI.
The Web framework for perfectionists with deadlines.
Pros of Django
- Comprehensive ecosystem with built-in admin interface, authentication, and many third-party packages
- Extensive documentation and large community support
- Robust ORM with support for complex queries and relationships
Cons of Django
- Steeper learning curve for beginners due to its full-stack nature
- Can be overkill for smaller projects or microservices
- Slower development cycle for simple CRUD operations compared to Piccolo
Code Comparison
Django ORM query:
from myapp.models import Book
books = Book.objects.filter(author__name='John Doe').order_by('-publication_date')
Piccolo ORM query:
from myapp.tables import Book
books = await Book.select().where(Book.author.name == 'John Doe').order_by(Book.publication_date, ascending=False)
Both Django and Piccolo provide powerful ORM capabilities, but Piccolo's syntax is more Pythonic and supports async operations out of the box. Django's ORM is more mature and offers a wider range of features, while Piccolo focuses on simplicity and performance. Django is better suited for large, complex projects, whereas Piccolo shines in smaller, async-focused applications.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Piccolo is a fast, user friendly ORM and query builder which supports asyncio. Read the docs.
Features
Some of itâs stand out features are:
- Support for sync and async.
- A builtin playground, which makes learning a breeze.
- Tab completion support - works great with iPython and VSCode.
- Batteries included - a User model, authentication, migrations, an admin GUI, and more.
- Modern Python - fully type annotated.
- Make your codebase modular and scalable with Piccolo apps (similar to Django apps).
Syntax
The syntax is clean and expressive.
You can use it as a query builder:
# Select:
await Band.select(
Band.name
).where(
Band.popularity > 100
)
# Join:
await Band.select(
Band.name,
Band.manager.name
)
# Delete:
await Band.delete().where(
Band.popularity < 1000
)
# Update:
await Band.update({Band.popularity: 10000}).where(
Band.name == 'Pythonistas'
)
Or like a typical ORM:
# To create a new object:
b = Band(name='C-Sharps', popularity=100)
await b.save()
# To fetch an object from the database, and update it:
b = await Band.objects().get(Band.name == 'Pythonistas')
b.popularity = 10000
await b.save()
# To delete:
await b.remove()
Installation
Installing with PostgreSQL driver:
pip install 'piccolo[postgres]'
Installing with SQLite driver:
pip install 'piccolo[sqlite]'
Installing with all optional dependencies (easiest):
pip install 'piccolo[all]'
Building a web app?
Let Piccolo scaffold you an ASGI web app, using Piccolo as the ORM:
piccolo asgi new
Starlette, FastAPI, BlackSheep, Litestar, Esmerald and Lilya are currently supported.
Are you a Django user?
We have a handy page which shows the equivalent of common Django queries in Piccolo.
Documentation
Our documentation is on Read the docs.
We also have some great tutorial videos on YouTube.
Top Related Projects
The Database Toolkit for Python
An async ORM. 🗃
Pony Object Relational Mapper
Familiar asyncio ORM for python, built with relations in mind
a small, expressive orm -- supports postgresql, mysql, sqlite and cockroachdb
The Web framework for perfectionists with deadlines.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot