Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
A variety of techniques to use with SQLAlchemy.
- Examples Included in the SQLAlchemy Distribution
- Schema Techniques
- ORM Configuration
- Create a data getter method, auto-map a column to it via a decorator
- Multiple Primary Mappers per Class (a.k.a how to do entity_name using SQLAlchemy 0.5 and above)
- eagerload-capable relationship to the "last"/"first" item etc
- Load polymorphically with polymorphic identity stored elsewhere
- How to map to PostgreSQL INHERITS partitions
- How to use CASE for polymorphic_on to produce more flexible polymorphic loading schemes
- Validation / Types
- Validate column-based attributes based on the type of column.
- Set up attribute validation/coercion events with all values of a certain type, typically a customized type
- A "case-insensitive-string" type illustrating filtered comparison operators
- Symmetric encryption/encoding using either a @hybrid property, or a TypeDecorator
- Database-side password crypt() using hybrid properties
- Mark mapped columns as "not updatable" or "not insertable"
- ORM Querying
- Session techniques
- How to efficiently delete "orphans" via a many-to-many relationship
- A recipe for ensuring the memory/database uniqueness of an object based on an attribute value
- Allow the ORM to automatically update relationship attributes when a foreign key attribute has changed
- Handy ways to temporarily disable autoflush
- Temporarily use a distinct session / make database calls within after_commit()
- Build a @property that's cached, expires when the database changes
- How to DELETE items in a collection before INSERT occurs, to maintain unique constraints
- Share user-defined state between a Session and its Connections; modify SQL, such as adding SQL comments or changing table names, on a per-Session/context basis
- Create in-memory "indexes" to find different kinds of objects in a Session
- Graphing / Documentation
- SQL Constructs
- Use window functions (ROW_NUMBER() OVER) for all LIMIT / OFFSET
- Add SQL comments to arbitrary elements of a SQL statement
- render a Postgresql VALUES expression (i.e. constant table)
- render a specific index of a Postgresql ARRAY expression
- create a Table-like object that is just like an existing Table, but different name
- From the Blog
- Django-style Database Routers in SQLAlchemy - (January 2012) Illustrates how to build rule-based database selection into the Session object.
- Value Agnostic Types, Part II - (October 2011) Integrating smart value objects at the type level using TypeDecorator.
- Hybrids and Value Agnostic Types, Part I - (October 2011) Creating smart value objects that integrate extremely well with the ORM.
- Magic, a "New" ORM (May 2011) Use declarative to create your own "mini-ORM", using whatever configurational patterns suit your needs.
- Storing/Using Enumerations (January 2011) Combines ENUM or CHAR with a nice in-Python usage pattern, replacing the need for cumbersome lookup tables.
- Polymorphic Associations (May 2007) An old article, explains the basics of an association from many different parents to a particular target.
Clone this wiki locally
Press h to open a hovercard with more details.