CARVIEW |
Select Language
HTTP/2 200
date: Thu, 17 Jul 2025 15:11:41 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
etag: W/"6196dfec561f244e23755f20308e93b3"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: no-referrer-when-downgrade
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=3Rqc9Nw7aXro0IiDCGqYPXlY9niMaXljh3P%2Fjr50Tm4rhZBRHiQIFEw9s3r4O1xBdTLFySeZV6Yoj0gcObFu%2Fk64xwx0In%2F%2BdrU1YImfkkmyVOjKi%2Fd7X6%2Bnla4S7AtR2W54Qkl%2B3sEcPcLm4Zzoh0fF9ZOmjKnDE%2F%2Bt0YClrIu1r2%2Bh%2BjvkeweRedtFooctqLah5JJb69Nj0vB8YK%2FRz18IsDXesFjWBkmHNfDo%2BLZ8WF1GcKqo5VJT%2FfiKh8o1GzJ6xdV91I%2BW4I8NC9o0Ig%3D%3D--ND6CwMxW7MFSRDyq--hnpzF7kFbWphPSwQ2rAg2A%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.1602449618.1752765101; Path=/; Domain=github.com; Expires=Fri, 17 Jul 2026 15:11:41 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Fri, 17 Jul 2026 15:11:41 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: DABE:2B4B5A:217C90:2653A6:687912AD
UsageRecipes · sqlalchemy/sqlalchemy Wiki · GitHub
Skip to content
Navigation Menu
{{ message }}
-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
UsageRecipes
mike bayer edited this page Jun 28, 2023
·
21 revisions
Theatrum Chemicum *
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
- Mark mapped columns as "not updatable" or "not insertable"
- Encryption
- Versioning
- ORM Querying
- Apply filtering criteria to all Query objects, such as a "public" column, a timestamp range, or similar
- Load related objects using a separate query, manually "knit" together results into collections on the parent object
- Iterate through a very large set of rows a chunk at a time, in O(N) time, i.e. not using LIMIT/OFFSET
- 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
- Add query-time information to loaded ORM instances, such as FOR UPDATE etc
- Graphing / Documentation
- SQL Constructs
- Warn for SELECT statements where tables in the FROM clause aren't joined together
- Use window functions (ROW_NUMBER() OVER) for all LIMIT / OFFSET
- Add SQL comments to arbitrary elements of a SQL statement
- SELECT...INTO
- 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
You can’t perform that action at this time.