Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

An easy-to-use and framework-independent light wrapper for SQLAlchemy

Project Description

A framework-independent wrapper for SQLAlchemy that makes it really easy and fun to use.

This library works with Python 2.6, 2.7 and pypy.



from orm import SQLALchemy

db = SQLALchemy('postgresql://scott:tiger@localhost:5432/mydatabase')

class ToDo(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(60), nullable=False)
done = db.Column(db.Boolean, nullable=False, default=False)
pub_date = db.Column(db.DateTime, nullable=False,

to_do = ToDo(title='Install orm', done=True)

completed = db.query(ToDo).order_by(Todo.pub_date.desc()).all()

It does an automatic table naming (if no table name is defined using the `__tablename__` property) by pluralizing the class name with the `inflection` library. So, for example, a `User` model gets a table named `users`.

How to use

The SQLAlchemy class is used to instantiate a SQLAlchemy connection to
a database.

db = SQLAlchemy(_uri_to_database_)

The class also provides access to all the SQLAlchemy
functions from the `sqlalchemy` and `sqlalchemy.orm` modules.
So you can declare models like this:

class User(db.Model):
login = db.Column(db.String(80), unique=True)
passw_hash = db.Column(db.String(80))
profile_id = db.Column(db.Integer, db.ForeignKey(
profile = db.relationship(Profile, backref=db.backref('user'))

In a web application you need to call `db.session.remove()` after each response, and `db.session.rollback()` if an error occurs. However, if you are using Flask or other framework that uses the `after_request` and `on_exception` decorators, these bindings can be done automatically (this works with Bottle's `hook` too):

app = Flask(__name__)

db = SQLAlchemy('sqlite://', app=app)


db = SQLAlchemy()

app = Flask(__name__)


More examples

Many databases, one web app

app = Flask(__name__)
db1 = SQLAlchemy(URI1, app)
db2 = SQLAlchemy(URI1, app)

Many web apps, one database

db = SQLAlchemy(URI1)

app1 = Flask(__name__)
app2 = Flask(__name__)

Aggegated selects

res = db.query(db.func.sum(Unit.price).label('price')).all()
print res.price

MIT License (
© 2012 by Lúcuma labs (
Release History

Release History

This version
History Node


History Node


History Node


History Node


History Node


History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
ORM-1.1.0.tar.gz (129.6 kB) Copy SHA256 Checksum SHA256 Source Feb 19, 2014

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting