Skip to main content

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 (

Project details

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
ORM-1.1.0.tar.gz (129.6 kB) Copy SHA256 hash SHA256 Source None Feb 19, 2014

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page