Essential SQLAlchemy

Book description

Essential SQLAlchemy introduces a high-level open-source code library that makes it easier for Python programmers to access relational databases such as Oracle, DB2, MySQL, PostgreSQL, and SQLite. SQLAlchemy has become increasingly popular since its release, but it still lacks good offline documentation. This practical book fills the gap, and because a developer wrote it, you get an objective look at SQLAlchemy's tools rather than an advocate's description of all the "cool" features.

SQLAlchemy includes both a database server-independent SQL expression language and an object-relational mapper (ORM) that lets you map "plain old Python objects" (POPOs) to database tables without substantially changing your existing Python code. Essential SQLAlchemy demonstrates how to use the library to create a simple database application, walks you through simple queries, and explains how to use SQLAlchemy to connect to multiple databases simultaneously with the same Metadata. You also learn how to:

  • Create custom types to be used in your schema, and when it's useful to use custom rather than built-in types
  • Run queries, updates, and deletes with SQLAlchemy's SQL expression language
  • Build an object mapper with SQLAlchemy, and understand the differences between this and active record patterns used in other ORMs
  • Create objects, save them to a session, and flush them to the database
  • Use SQLAlchemy to model object oriented inheritance
  • Provide a declarative, active record pattern for use with SQLAlchemy using the Elixir extension
  • Use the SQLSoup extension to provide an automatic metadata and object model based on database reflection

In addition, you'll learn how and when to use other extensions to SQLAlchemy, including AssociationProxy, OrderingList, and more.

Essential SQLAlchemy is the much-needed guide for every Python developer using this code library. Instead of a feature-by-feature documentation, this book takes an "essentials" approach that gives you exactly what you need to become productive with SQLAlchemy right away.

Publisher resources

View/Submit Errata

Table of contents

  1. Essential SQLAlchemy
  2. A Note Regarding Supplemental Files
  3. Preface
    1. Audience
    2. Assumptions This Book Makes
    3. Contents of This Book
    4. Conventions Used in This Book
    5. Using Code Examples
    6. How to Contact Us
    7. Acknowledgments
  4. 1. Introduction to SQLAlchemy
    1. What Is SQLAlchemy
    2. The Object/Relational “Impedance Mismatch”
    3. SQLAlchemy Philosophy
    4. SQLAlchemy Architecture
      1. Engine
        1. Connection pooling
        2. SQL dialect management
      2. MetaData Management
      3. Types System
      4. SQL Expression Language
      5. Object Relational Mapper (ORM)
  5. 2. Getting Started
    1. Installing SQLAlchemy
      1. Installing the SQLAlchemy Package
        1. Installing setup tools
        2. Installing SQLAlchemy with easy_install
        3. Testing the install
      2. Installing Some Database Drivers
        1. Installing the SQLite driver on Python versions before 2.5
        2. Other supported drivers
    2. SQLAlchemy Tutorial
      1. Connecting to the Database and Creating Some Tables
      2. Performing Queries and Updates
      3. Mapping Objects to Tables
  6. 3. Engines and MetaData
    1. Engines and Connectables
      1. Configuring SQLAlchemy Logging
      2. Database Connections and ResultProxys
      3. Connection Pooling
    2. MetaData
      1. Getting Started with MetaData
      2. Defining Tables
        1. Table reflection
      3. Column Definitions
      4. Constraints
        1. Primary keys
        2. Foreign keys
        3. UNIQUE constraints
        4. CHECK constraints
      5. Defaults
        1. Active defaults
        2. Passive defaults
      6. Defining Indexes
        1. The Index object
      7. Creating Explicit Sequences
      8. MetaData Operations
        1. Binding MetaData
        2. Create/drop MetaData and schema objects
        3. Adapt Tables from one MetaData to another
  7. 4. SQLAlchemy Type Engines
    1. Type System Overview
    2. Built-in Types
      1. Generic Types
      2. Dialect-Specific Types
    3. Application-Specific Custom Types
      1. Implementing a TypeDecorator
      2. Creating a New TypeEngine
  8. 5. Running Queries and Updates
    1. Inserts, Updates, and Deletes
      1. Insert Statements
      2. Update Statements
      3. Delete Statements
    2. Queries
      1. Basic Query Construction
        1. The select() function versus the select() method
        2. Result set objects
        3. Operators and functions in WHERE clauses
        4. Using custom bind parameters
        5. Using literal text in queries
        6. Ordering and grouping results, returning distinct values
        7. Limiting results returned
        8. Using the “generative” query interface
      2. Joins and Set Operations
        1. Joining selectables
        2. Set operations (UNION, INTERSECT, EXCEPT)
        3. Using aliases
      3. Subqueries
        1. Embedding subqueries in the column list
        2. Correlated versus uncorrelated subqueries
        3. Embedding subqueries in an IN clause
        4. Embedding subqueries in the FROM clause
  9. 6. Building an Object Mapper
    1. Introduction to ORMs
      1. Design Concepts in the ORM
        1. The data mapper pattern
        2. The unit of work pattern
    2. Declaring Object Mappers
      1. Basic Object Mapping
      2. Customizing Property Mapping
        1. Using include_properties and exclude_properties
        2. Customizing the name of the mapped column
        3. Using synonyms
        4. Mapping subqueries
        5. Mapping composite values
        6. Eager versus deferred loading
      3. Mapping Arbitrary Selectables
      4. Other mapper() Parameters
    3. Declaring Relationships Between Mappers
      1. Basic Relationships
        1. 1:N relations
        2. M:N relations
        3. 1:1 relations
      2. Using BackRefs
      3. Using a Self-Referential Mapper
      4. Cascading Changes to Related Objects
      5. Other relation() and backref() Parameters
        1. Using custom collections in relations
    4. Extending Mappers
    5. ORM Partitioning Strategies
      1. Vertical Partitioning
      2. Horizontal Partitioning
  10. 7. Querying and Updating at the ORM Level
    1. The SQLAlchemy ORM Session Object
      1. Creating a Session
      2. Saving Objects to the Session
      3. Updating Objects in the Session
        1. Embedding SQL expressions in a flush
      4. Deleting Objects from the Session
      5. Flushing, Committing, and Rolling Back Session Changes
      6. Other Session Methods
      7. Extending Sessions
    2. Querying at the ORM Level
      1. ORM Querying with Joins
      2. Customizing the Select Statement in ORM Queries
      3. Other Query Methods
    3. Contextual or Thread-Local Sessions
      1. Using Contextual Sessions with Mappers and Classes
  11. 8. Inheritance Mapping
    1. Overview of Inheritance Mapping
    2. Single Table Inheritance Mapping
    3. Concrete Table Inheritance Mapping
    4. Joined Table Inheritance Mapping
      1. Optimizing Performance with Joined Table Inheritance Mapping
        1. Using deferred loading
        2. Using select_table
    5. Relations and Inheritance
  12. 9. Elixir: A Declarative Extension to SQLAlchemy
    1. Introduction to Elixir
    2. Installing Elixir
    3. Using Elixir
      1. Fields and Properties
        1. Elixir deferred properties
      2. Relations
        1. Attribute-based syntax
        2. DSL syntax
      3. Inheritance
      4. Querying Using Elixir
    4. Elixir Extensions
      1. Associable Extension
      2. Encrypted Extension
      3. Versioned Extension
  13. 10. SqlSoup: An Automatic Mapper for SQLAlchemy
    1. Introduction to SqlSoup
    2. Using SqlSoup for ORM-Style Queries and Updates
      1. Joins with SqlSoup
      2. Mapping Arbitrary Selectables
      3. Directly Accessing the Session
    3. Using SqlSoup for SQL-Level Inserts, Updates, and Deletes
    4. When to Use SqlSoup Versus Elixir Versus “Bare” SQLAlchemy
      1. SqlSoup Pros and Cons
      2. Elixir Pros and Cons
  14. 11. Other SQLAlchemy Extensions
    1. Association Proxy
    2. Ordering List
    3. Deprecated Extensions
  15. Index
  16. About the Author
  17. Colophon
  18. Copyright

Product information

  • Title: Essential SQLAlchemy
  • Author(s): Rick Copeland
  • Release date: June 2008
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596516147