Overview / Installation
============

## Overview

The SQLAlchemy SQL Toolkit and Object Relational Mapper is a comprehensive set of tools for working with databases and Python.  It has several distinct areas of functionality which can be used individually or combined together.  Its major API components, all public-facing, are illustrated below:

    {diagram}
               +-----------------------------------------------------------+
               |             Object Relational Mapper (ORM)                |
               |                [[tutorial]](rel:datamapping)    [[docs]](rel:advdatamapping)                       |
               +-----------------------------------------------------------+
               +---------+ +------------------------------------+ +--------+
               |         | |       SQL Expression Language      | |        |
               |         | |        [[tutorial]](rel:sql)  [[docs]](rel:docstrings_sqlalchemy.sql.expression)          | |        |
               |         | +------------------------------------+ |        |
               |         +-----------------------+ +--------------+        |
               |        Dialect/Execution        | |    Schema Management  |
               |              [[docs]](rel:dbengine)             | |        [[docs]](rel:metadata)         |
               +---------------------------------+ +-----------------------+
               +----------------------+ +----------------------------------+
               |  Connection Pooling  | |              Types               |
               |        [[docs]](rel:pooling)        | |              [[docs]](rel:types)              |
               +----------------------+ +----------------------------------+

Above, the two most significant front-facing portions of SQLAlchemy are the **Object Relational Mapper** and the **SQL Expression Language**.  These are two separate toolkits, one building off the other.  SQL Expressions can be used independently of the ORM.  When using the ORM, the SQL Expression language is used to establish object-relational configurations as well as in querying.

## Tutorials

 * [Object Relational Tutorial](rel:datamapping) - This describes the richest feature of SQLAlchemy, its object relational mapper.  If you want to work with higher-level SQL which is constructed automatically for you, as well as management of Python objects, proceed to this tutorial.
 * [SQL Expression Tutorial](rel:sql) - The core of SQLAlchemy is its SQL expression language.  The SQL Expression Language is a toolkit all its own, independent of the ORM package, which can be used to construct manipulable SQL expressions which can be programmatically constructed, modified, and executed, returning cursor-like result sets.  It's a lot more lightweight than the ORM and is appropriate for higher scaling SQL operations.  It's also heavily present within the ORM's public facing API, so advanced ORM users will want to master this language as well.

## Reference Documentation

 * [Datamapping](rel:advdatamapping) - A comprehensive walkthrough of major ORM patterns and techniques.
 * [Session](rel:unitofwork) - A detailed description of SQLAlchemy's Session object
 * [Engines](rel:dbengine) - Describes SQLAlchemy's database-connection facilities, including connection documentation and working with connections and transactions. 
 * [Connection Pools](rel:pooling) - Further detail about SQLAlchemy's connection pool library.
 * [Metadata](rel:metadata) - All about schema management using `MetaData` and `Table` objects; reading database schemas into your application, creating and dropping tables, constraints, defaults, sequences, indexes.
 * [Types](rel:types) - Datatypes included with SQLAlchemy, their functions, as well as how to create your own types.
 * [Plugins](rel:plugins) - Included addons for SQLAlchemy

## Installing SQLAlchemy {@name=sqlalchemy}

Installing SQLAlchemy from scratch is most easily achieved with [setuptools][].  ([setuptools installation][install setuptools]). Just run this from the command-line:
    
    # easy_install SQLAlchemy

This command will download the latest version of SQLAlchemy from the [Python Cheese Shop][pypi] and install it to your system.

[setuptools]: http://peak.telecommunity.com/DevCenter/setuptools
[install setuptools]: http://peak.telecommunity.com/DevCenter/EasyInstall#installation-instructions
[pypi]: http://pypi.python.org/pypi/SQLAlchemy

Otherwise, you can install from the distribution using the `setup.py` script:

    # python setup.py install

### Installing a Database API {@name=dbms}

SQLAlchemy is designed to operate with a [DB-API](http://www.python.org/doc/peps/pep-0249/) implementation built for a particular database, and includes support for the most popular databases:

* Postgres:  [psycopg2](http://www.initd.org/tracker/psycopg/wiki/PsycopgTwo)
* SQLite:  [pysqlite](http://initd.org/tracker/pysqlite), [sqlite3](http://docs.python.org/lib/module-sqlite3.html) (included with Python 2.5 or greater)
* MySQL:   [MySQLdb](http://sourceforge.net/projects/mysql-python)
* Oracle:  [cx_Oracle](http://www.cxtools.net/default.aspx?nav=home)
* MS-SQL:  [pyodbc](http://pyodbc.sourceforge.net/) (recommended), [adodbapi](http://adodbapi.sourceforge.net/)  or [pymssql](http://pymssql.sourceforge.net/)
* Firebird:  [kinterbasdb](http://kinterbasdb.sourceforge.net/)
* Informix:  [informixdb](http://informixdb.sourceforge.net/)

### Checking the Installed SQLAlchemy Version
 
This documentation covers SQLAlchemy version 0.4.  If you're working on a system that already has SQLAlchemy installed, check the version from your Python prompt like this:

     {python}
     >>> import sqlalchemy
     >>> sqlalchemy.__version__ # doctest: +SKIP
     0.4.0

## 0.3 to 0.4 Migration {@name=migration}

From version 0.3 to version 0.4 of SQLAlchemy, some conventions have changed.  Most of these conventions are available in the most recent releases of the 0.3 series starting with version 0.3.9, so that you can make a 0.3 application compatible with 0.4 in most cases.

This section will detail only those things that have changed in a backwards-incompatible manner.  For a full overview of everything that's new and changed, see [WhatsNewIn04](http://www.sqlalchemy.org/trac/wiki/WhatsNewIn04).

### ORM Package is now sqlalchemy.orm {@name=imports}

All symbols related to the SQLAlchemy Object Relational Mapper, i.e. names like `mapper()`, `relation()`, `backref()`, `create_session()` `synonym()`, `eagerload()`, etc. are now only in the `sqlalchemy.orm` package, and **not** in `sqlalchemy`.  So if you were previously importing everything on an asterisk:

    {python}
    from sqlalchemy import *
    
You should now import separately from orm:

    {python}
    from sqlalchemy import *
    from sqlalchemy.orm import *
    
Or more commonly, just pull in the names you'll need:

    {python}
    from sqlalchemy import create_engine, MetaData, Table, Column, types
    from sqlalchemy.orm import mapper, relation, backref, create_session

### BoundMetaData is now MetaData {@name=metadata}

The `BoundMetaData` name is removed.  Now, you just use `MetaData`.  Additionally, the `engine` parameter/attribute is now called `bind`, and `connect()` is deprecated:

    {python}
    # plain metadata
    meta = MetaData()
    
    # metadata bound to an engine
    meta = MetaData(engine)
    
    # bind metadata to an engine later
    meta.bind = engine
    
Additionally, `DynamicMetaData` is now known as `ThreadLocalMetaData`.

### Some existing select() methods become generative {@name=generative}

The methods `correlate()`, `order_by()`, and `group_by()` on the `select()` construct now return a **new** select object, and do not change the original one.  Additionally, the generative methods `where()`, `column()`, `distinct()`, and several others have been added:

    {python}
    s = table.select().order_by(table.c.id).where(table.c.x==7)
    result = engine.execute(s)

### collection_class behavior is changed {@name=collection}

If you've been using the `collection_class` option on `mapper()`, the requirements for instrumented collections have changed.  For an overview, see [advdatamapping_relation_collections](rel:advdatamapping_relation_collections).

### All "engine", "bind_to", "connectable" Keyword Arguments Changed to "bind" {@name=bind}

This is for create/drop statements, sessions, SQL constructs, metadatas:

    {python}
    myengine = create_engine('sqlite://')

    meta = MetaData(myengine)

    meta2 = MetaData()
    meta2.bind = myengine

    session = create_session(bind=myengine)

    statement = select([table], bind=myengine)
    
    meta.create_all(bind=myengine)
    
### All "type" Keyword Arguments Changed to "type_" {@name=type}

This mostly applies to SQL constructs where you pass a type in:

    {python}
    s = select([mytable], mytable.c.x=bindparam(y, type_=DateTime))
    
    func.now(type_=DateTime)
    
### Mapper Extensions must return EXT_CONTINUE to continue execution to the next mapper

If you extend the mapper, the methods in your mapper extension must return EXT_CONTINUE to continue executing additional mappers.
