Convert Figma logo to code with AI

ponyorm logopony

Pony Object Relational Mapper

3,606
243
3,606
336

Top Related Projects

The Database Toolkit for Python

79,088

The Web framework for perfectionists with deadlines.

1,775

An async ORM. 🗃

Familiar asyncio ORM for python, built with relations in mind

11,053

a small, expressive orm -- supports postgresql, mysql, sqlite and cockroachdb

38,831

Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB

Quick Overview

Pony ORM is a Python Object-Relational Mapping (ORM) library that provides a powerful and intuitive way to interact with databases. It offers a unique approach to database querying with its own query language, which is translated into efficient SQL queries.

Pros

  • Intuitive and expressive query syntax
  • Automatic database schema generation and migration
  • Support for multiple database backends (SQLite, PostgreSQL, MySQL, Oracle)
  • Strong typing and compile-time checks for queries

Cons

  • Steeper learning curve compared to some other ORMs
  • Smaller community and ecosystem compared to SQLAlchemy or Django ORM
  • Limited support for NoSQL databases
  • May have performance overhead for very complex queries

Code Examples

  1. Defining entities:
from pony.orm import *

db = Database()

class Person(db.Entity):
    name = Required(str)
    age = Required(int)
    cars = Set('Car')

class Car(db.Entity):
    make = Required(str)
    model = Required(str)
    owner = Required(Person)

db.bind(provider='sqlite', filename='database.sqlite', create_db=True)
db.generate_mapping(create_tables=True)
  1. Inserting data:
@db_session
def create_data():
    p1 = Person(name='John', age=30)
    Car(make='Toyota', model='Camry', owner=p1)
    Car(make='Honda', model='Civic', owner=p1)

create_data()
  1. Querying data:
@db_session
def query_data():
    # Find all persons older than 25 with a Toyota car
    result = select(p for p in Person if p.age > 25 and 'Toyota' in p.cars.make)
    for person in result:
        print(f"{person.name} owns a Toyota")

query_data()

Getting Started

  1. Install Pony ORM:

    pip install pony
    
  2. Create a new Python file and import Pony ORM:

    from pony.orm import *
    
  3. Define your database and entities:

    db = Database()
    
    class YourEntity(db.Entity):
        field1 = Required(str)
        field2 = Optional(int)
    
    db.bind(provider='sqlite', filename='database.sqlite', create_db=True)
    db.generate_mapping(create_tables=True)
    
  4. Use the @db_session decorator for database operations:

    @db_session
    def your_function():
        # Perform database operations here
        pass
    

Competitor Comparisons

The Database Toolkit for Python

Pros of SQLAlchemy

  • More mature and widely adopted, with extensive documentation and community support
  • Offers greater flexibility and control over database operations
  • Supports a wider range of databases and advanced features

Cons of SQLAlchemy

  • Steeper learning curve, especially for beginners
  • More verbose syntax compared to Pony ORM
  • Can be overkill for simple database operations

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)

Pony ORM:

from pony.orm import *

db = Database()

class User(db.Entity):
    id = PrimaryKey(int, auto=True)
    name = Required(str)

SQLAlchemy requires more boilerplate code and explicit column definitions, while Pony ORM offers a more concise and intuitive syntax. However, SQLAlchemy's verbosity allows for greater customization and control over the database schema and operations.

79,088

The Web framework for perfectionists with deadlines.

Pros of Django

  • More comprehensive web framework with built-in admin interface, authentication, and other features
  • Larger community and ecosystem, with extensive third-party packages and resources
  • Better suited for large-scale, complex web applications

Cons of Django

  • Steeper learning curve due to its full-stack nature and larger codebase
  • Can be overkill for smaller projects or simple APIs
  • ORM is less intuitive and requires more boilerplate code compared to Pony ORM

Code Comparison

Django ORM:

from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField()

Pony ORM:

from pony.orm import *

class Person(db.Entity):
    name = Required(str)
    age = Required(int)

Django's ORM requires more verbose code and explicit field types, while Pony ORM offers a more concise syntax with automatic type inference. Pony ORM also provides a more Pythonic approach to defining entity relationships and querying data. However, Django's ORM is more widely used and has better integration with the rest of the Django ecosystem.

1,775

An async ORM. 🗃

Pros of ORM

  • Supports async/await syntax, making it well-suited for modern asynchronous Python applications
  • Integrates seamlessly with FastAPI and other ASGI frameworks
  • Offers a more Pythonic query syntax, closer to standard Python expressions

Cons of ORM

  • Less mature and less widely adopted compared to Pony ORM
  • Documentation is not as comprehensive as Pony ORM's
  • May have a steeper learning curve for developers new to ORMs

Code Comparison

Pony ORM:

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)

ORM:

import databases
import sqlalchemy

database = databases.Database("sqlite:///database.sqlite")
metadata = sqlalchemy.MetaData()

persons = sqlalchemy.Table(
    "persons", metadata,
    sqlalchemy.Column("id", sqlalchemy.Integer, primary_key=True),
    sqlalchemy.Column("name", sqlalchemy.String),
    sqlalchemy.Column("age", sqlalchemy.Integer),
)

Both ORMs provide powerful database abstraction layers, but they differ in syntax and approach. Pony ORM offers a more declarative style, while ORM leans towards a more explicit configuration. The choice between them often depends on the specific project requirements and developer preferences.

Familiar asyncio ORM for python, built with relations in mind

Pros of Tortoise-ORM

  • Asynchronous: Built for async/await, ideal for high-performance applications
  • SQLAlchemy-like syntax: Familiar to many Python developers
  • Supports multiple databases: Works with PostgreSQL, MySQL, SQLite, and more

Cons of Tortoise-ORM

  • Steeper learning curve: More complex setup and configuration
  • Less intuitive query syntax: Requires more verbose code for some operations
  • Younger project: Less mature ecosystem and community support

Code Comparison

Tortoise-ORM:

from tortoise import fields, models

class User(models.Model):
    name = fields.CharField(max_length=50)
    age = fields.IntField()

Pony:

from pony.orm import *

class User(db.Entity):
    name = Required(str, 50)
    age = Required(int)

Tortoise-ORM uses a class-based approach similar to Django's ORM, while Pony uses a more concise syntax with custom types. Pony's setup is generally simpler and more intuitive for beginners, but Tortoise-ORM's async capabilities make it more suitable for high-performance applications. Both ORMs offer powerful querying capabilities, but Pony's query syntax is often more readable and Pythonic. Tortoise-ORM's multi-database support gives it an edge in flexibility, while Pony's maturity and extensive documentation make it easier to work with for many developers.

11,053

a small, expressive orm -- supports postgresql, mysql, sqlite and cockroachdb

Pros of Peewee

  • More mature and widely adopted, with a larger community and ecosystem
  • Supports a broader range of databases, including SQLite, MySQL, and PostgreSQL
  • Offers more advanced features like migrations and connection pooling

Cons of Peewee

  • Slightly more verbose syntax compared to Pony's declarative style
  • Lacks some of Pony's advanced features like automatic query optimization

Code Comparison

Peewee:

from peewee import *

db = SqliteDatabase('people.db')

class Person(Model):
    name = CharField()
    age = IntegerField()

    class Meta:
        database = db

Pony:

from pony.orm import *

db = Database()

class Person(db.Entity):
    name = Required(str)
    age = Required(int)

db.bind(provider='sqlite', filename='people.db')

Both Peewee and Pony are Python ORM libraries, but they differ in syntax and features. Peewee uses a more traditional ORM approach, while Pony offers a more declarative and intuitive syntax. Pony also provides automatic query optimization and a unique querying language, which can be advantageous for complex queries. However, Peewee's wider adoption and broader database support make it a more versatile choice for many projects.

38,831

Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB

Pros of Prisma

  • More extensive database support, including PostgreSQL, MySQL, SQLite, and MongoDB
  • Stronger TypeScript integration with auto-generated types
  • More active development and larger community support

Cons of Prisma

  • Steeper learning curve, especially for developers new to ORMs
  • Requires a separate schema file, which can add complexity to project structure

Code Comparison

Prisma:

const user = await prisma.user.create({
  data: {
    name: 'Alice',
    email: 'alice@example.com',
  },
})

Pony:

user = User(name='Alice', email='alice@example.com')
commit()

Key Differences

  • Prisma uses a schema-first approach, while Pony uses a code-first approach
  • Prisma has a more complex setup process but offers more advanced features
  • Pony provides a more Pythonic syntax and is generally easier for Python developers to adopt

Use Cases

  • Prisma: Ideal for large-scale applications with complex data models and TypeScript projects
  • Pony: Better suited for smaller Python projects or those requiring a simpler ORM solution

Community and Ecosystem

Prisma has a larger and more active community, with more frequent updates and extensive documentation. Pony, while less popular, has a dedicated user base and is known for its simplicity and ease of use in the Python ecosystem.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Downloads

Downloads Downloads Downloads

Pony Object-Relational Mapper

Pony is an advanced object-relational mapper. The most interesting feature of Pony is its ability to write queries to the database using Python generator expressions and lambdas. Pony analyzes the abstract syntax tree of the expression and translates it into a SQL query.

Here is an example query in Pony:

select(p for p in Product if p.name.startswith('A') and p.cost <= 1000)

Pony translates queries to SQL using a specific database dialect. Currently Pony works with SQLite, MySQL, PostgreSQL and Oracle databases.

By providing a Pythonic API, Pony facilitates fast app development. Pony is an easy-to-learn and easy-to-use library. It makes your work more productive and helps to save resources. Pony achieves this ease of use through the following:

  • Compact entity definitions
  • The concise query language
  • Ability to work with Pony interactively in a Python interpreter
  • Comprehensive error messages, showing the exact part where an error occurred in the query
  • Displaying of the generated SQL in a readable format with indentation

All this helps the developer to focus on implementing the business logic of an application, instead of struggling with a mapper trying to understand how to get the data from the database.

See the example here

Support Pony ORM Development

Pony ORM is Apache 2.0 licensed open source project. If you would like to support Pony ORM development, please consider:

Become a backer or sponsor

Online tool for database design

Pony ORM also has the Entity-Relationship Diagram Editor which is a great tool for prototyping. You can create your database diagram online at https://editor.ponyorm.com, generate the database schema based on the diagram and start working with the database using declarative queries in seconds.

Documentation

Documentation is available at https://docs.ponyorm.org The documentation source is available at https://github.com/ponyorm/pony-doc. Please create new documentation related issues here or make a pull request with your improvements.

License

Pony ORM is released under the Apache 2.0 license.

PonyORM community

Please post your questions on Stack Overflow. Meet the PonyORM team, chat with the community members, and get your questions answered on our community Telegram group. Join our newsletter at ponyorm.org. Reach us on Twitter.

Copyright (c) 2013-2022 Pony ORM. All rights reserved. info (at) ponyorm.org