SQLAlchemy Example Projects and Code

SQLAlchemy is a well-regarded database toolkit and object-relational mapper (ORM) implementation written in Python. SQLAlchemy provides a generalized interface for creating and executing database-agnostic code without needing to write SQL statements. SQLAlchemy logo.

Code Examples Sorted by SQLAlchemy Package

Specific examples are shown on the following individual pages, organized by classes and functions provided by SQLAlchemy, that are typically used when building your own applications.

SQLAlchemy: Extensions, Plug-ins and Related Libraries & Example Projects and Code

sqlalchemy.databases mysql

sqlalchemy.dialects mssql, mysql, oracle, postgresql, sqlite

sqlalchemy.dialects.mysql pymysql

sqlalchemy.dialects.postgresql ARRAY, BIGINT, BIT, DOUBLE_PRECISION, ExcludeConstraint, INTEGER, JSON, TSVECTOR, array, json, pypostgresql

sqlalchemy.dialects.postgresql.base PGCompiler, PGIdentifierPreparer, PGTypeCompiler

sqlalchemy.dialects.postgresql.psycopg2 PGDialect_psycopg2

sqlalchemy.dialects.sqlite pysqlite

sqlalchemy.engine Connection, Engine, create_engine, default, url

sqlalchemy.engine.default DefaultDialect

sqlalchemy.engine.interfaces ExecutionContext

sqlalchemy.engine.result ResultMetaData, RowProxy

sqlalchemy.engine.strategies EngineStrategy, MockEngineStrategy

sqlalchemy.engine.url make_url

sqlalchemy.events SchemaEventTarget

sqlalchemy.exc ArgumentError, DataError, DatabaseError, IntegrityError, InvalidRequestError, NoInspectionAvailable, NoSuchTableError, OperationalError, ProgrammingError, UnsupportedCompilationError

sqlalchemy.ext compiler

sqlalchemy.ext.associationproxy AssociationProxy

sqlalchemy.ext.automap automap_base

sqlalchemy.ext.compiler compiles

sqlalchemy.ext.declarative DeclarativeMeta, declarative_base

sqlalchemy.ext.hybrid HYBRID_METHOD, HYBRID_PROPERTY, hybrid_method, hybrid_property

sqlalchemy.ext.mutable Mutable

sqlalchemy.inspection inspect

sqlalchemy.orm ColumnProperty, CompositeProperty, Load, Mapper, Query, RelationshipProperty, Session, SynonymProperty, aliased, attributes, backref, class_mapper, column_property, composite, interfaces, mapper, mapperlib, object_mapper, object_session, query, relationship, session, sessionmaker, strategies

sqlalchemy.orm.attributes InstrumentedAttribute, QueryableAttribute, flag_modified

sqlalchemy.orm.collections InstrumentedList

sqlalchemy.orm.exc NoResultFound, UnmappedClassError, UnmappedInstanceError

sqlalchemy.orm.interfaces MapperProperty, PropComparator

sqlalchemy.orm.mapper Mapper

sqlalchemy.orm.properties ColumnProperty, RelationshipProperty

sqlalchemy.orm.query Query, QueryContext

sqlalchemy.orm.relationships RelationshipProperty

sqlalchemy.orm.session Session, object_session

sqlalchemy.orm.util AliasedClass, AliasedInsp, identity_key

sqlalchemy.pool NullPool, StaticPool

sqlalchemy.schema CheckConstraint, Column, CreateIndex, CreateTable, DDLElement, ForeignKey, ForeignKeyConstraint, Index, PrimaryKeyConstraint, Table

sqlalchemy.sql ClauseElement, Select, column, expression, extract, functions, operators, schema, select, sqltypes, table

sqlalchemy.sql.compiler SQLCompiler

sqlalchemy.sql.elements ColumnElement, Label

sqlalchemy.sql.expression ClauseElement, ColumnClause, ColumnElement, Executable, FunctionElement, UnaryExpression

sqlalchemy.sql.functions FunctionElement, GenericFunction

sqlalchemy.sql.naming conv

sqlalchemy.sql.schema Column, SchemaItem

sqlalchemy.sql.sqltypes NULLTYPE, NullType

sqlalchemy.sql.util ClauseAdapter

sqlalchemy.sql.visitors traverse

sqlalchemy.types BOOLEAN, Boolean, DATE, DATETIME, Date, DateTime, Enum, FLOAT, Float, INTEGER, Integer, Interval, NULLTYPE, NullType, String, TEXT, TIME, Text, Time, TypeEngine, UserDefinedType, to_instance

sqlalchemy.util OrderedDict, OrderedSet, set_creation_order, symbol, topological

sqlalchemy.util.langhelpers public_factory, symbol

Example Open Source Projects

The following open source projects can serve as example code for you as you build your own applications with SQLAlchemy.

databases

databases (project homepage and PyPI page provides asyncio support with an SQLALchemy Core interface for common relational databases such as MySQL, PostgreSQL and SQLite. This is handy for integrating with asynchronous I/O web frameworks like Sanic. The project is open sourced under the BSD 3-Clause "New" or "Revised" License.

CTFd

CTFd (homepage) is a capture the flag (CTF) hacking web app built with SQLAlchemy and Flask. The application can be used as-is to run CTF events, or the code can be modified for custom rules on hacking scenarios. CTFd is open sourced under the
Apache License 2.0.

indico

indico (project website, documentation and sandbox demo) is a Flask-based web app for event management that is powered by SQLAlchemy on the backend. The code for this project is open sourced under the MIT license.

sandman2

sandman2 (project documentation and PyPI package information) is a code library for automatically generating RESTful APIs from existing database schemas. This approach is handy for solving straightforward situations where you want to put an abstraction layer between one or more applications and your relational database to prevent or reduce direct database access.

The sandman2 project is provided under the Apache License 2.0.

1. Introduction 2. Development Environments 3. Data 4. Web Development 5. Deployment 6. DevOps Changelog What Full Stack Means About the Author Future Directions Page Statuses SQLAlchemy ExtensionsSQLAlchemy Example CodeSQLAlchemy Modelssqlalchemy.databases mysqlsqlalchemy.dialects mssqlsqlalchemy.dialects mysqlsqlalchemy.dialects oraclesqlalchemy.schema DDLsqlalchemy.dialects postgresqlsqlalchemy.sql.expression Functionsqlalchemy.dialects sqlitesqlalchemy.dialects.mysql pymysqlsqlalchemy.dialects.postgresql ARRAYsqlalchemy.dialects.postgresql BIGINTsqlalchemy.dialects.postgresql BITsqlalchemy.dialects.postgresql DOUBLE_PRECISIONsqlalchemy.dialects.postgresql ExcludeConstraintsqlalchemy.dialects.postgresql INTEGERsqlalchemy.dialects.postgresql JSONsqlalchemy.dialects.postgresql TSVECTORsqlalchemy.dialects.postgresql pypostgresqlsqlalchemy.dialects.postgresql.base PGCompilersqlalchemy.dialects.postgresql.base PGIdentifierPreparersqlalchemy.dialects.postgresql.base PGTypeCompilersqlalchemy.dialects.postgresql.psycopg2 PGDialect_psycopg2sqlalchemy.dialects.sqlite pysqlitesqlalchemy.engine Connectionsqlalchemy.engine Enginesqlalchemy.engine create_enginesqlalchemy.engine defaultsqlalchemy.engine urlsqlalchemy.engine.default DefaultDialectsqlalchemy.engine.interfaces ExecutionContextsqlalchemy.engine.result ResultMetaDatasqlalchemy.engine.result RowProxysqlalchemy.engine.strategies EngineStrategysqlalchemy.engine.strategies MockEngineStrategysqlalchemy.engine.url make_urlsqlalchemy.events SchemaEventTargetsqlalchemy.exc ArgumentErrorsqlalchemy.exc DataErrorsqlalchemy.exc DatabaseErrorsqlalchemy.exc IntegrityErrorsqlalchemy.exc InvalidRequestErrorsqlalchemy.exc NoInspectionAvailablesqlalchemy.exc NoSuchTableErrorsqlalchemy.exc OperationalErrorsqlalchemy.exc ProgrammingErrorsqlalchemy.exc UnsupportedCompilationErrorsqlalchemy.ext compilersqlalchemy.ext.associationproxy AssociationProxysqlalchemy.ext.automap automap_basesqlalchemy.ext.compiler compilessqlalchemy.ext.declarative DeclarativeMetasqlalchemy.ext.declarative declarative_basesqlalchemy.ext.hybrid hybrid_methodsqlalchemy.ext.hybrid hybrid_propertysqlalchemy.ext.mutable Mutablesqlalchemy.inspection inspectsqlalchemy.orm ColumnPropertysqlalchemy.orm CompositePropertysqlalchemy.orm Loadsqlalchemy.orm Querysqlalchemy.orm RelationshipPropertysqlalchemy.orm SynonymPropertysqlalchemy.orm aliasedsqlalchemy.orm attributessqlalchemy.orm backrefsqlalchemy.orm class_mappersqlalchemy.orm column_propertysqlalchemy.orm compositesqlalchemy.orm interfacessqlalchemy.orm mappersqlalchemy.orm mapperlibsqlalchemy.orm object_mappersqlalchemy.orm object_sessionsqlalchemy.orm relationshipsqlalchemy.orm sessionsqlalchemy.orm sessionmakersqlalchemy.orm strategiessqlalchemy.orm.attributes InstrumentedAttributesqlalchemy.orm.attributes QueryableAttributesqlalchemy.orm.attributes flag_modifiedsqlalchemy.orm.collections InstrumentedListsqlalchemy.orm.exc NoResultFoundsqlalchemy.orm.exc UnmappedClassErrorsqlalchemy.orm.exc UnmappedInstanceErrorsqlalchemy.orm.interfaces MapperPropertysqlalchemy.orm.interfaces PropComparatorsqlalchemy.orm.mapper Mappersqlalchemy.orm.properties ColumnPropertysqlalchemy.orm.properties RelationshipPropertysqlalchemy.orm.query Querysqlalchemy.orm.query QueryContextsqlalchemy.orm.relationships RelationshipPropertysqlalchemy.orm.session Sessionsqlalchemy.orm.session object_sessionsqlalchemy.orm.util AliasedClasssqlalchemy.orm.util AliasedInspsqlalchemy.orm.util identity_keysqlalchemy.pool NullPoolsqlalchemy.pool StaticPoolsqlalchemy.schema CheckConstraintsqlalchemy.schema Columnsqlalchemy.schema CreateIndexsqlalchemy.schema CreateTablesqlalchemy.schema DDLElementsqlalchemy.schema ForeignKeysqlalchemy.schema ForeignKeyConstraintsqlalchemy.schema Indexsqlalchemy.schema PrimaryKeyConstraintsqlalchemy.schema Tablesqlalchemy.sql ClauseElementsqlalchemy.sql columnsqlalchemy.sql expressionsqlalchemy.sql extractsqlalchemy.sql functionssqlalchemy.sql operatorssqlalchemy.sql schemasqlalchemy.sql selectsqlalchemy.sql sqltypessqlalchemy.sql tablesqlalchemy.sql.compiler SQLCompilersqlalchemy.sql.elements ColumnElementsqlalchemy.sql.elements Labelsqlalchemy.sql.expression ClauseElementsqlalchemy.sql.expression ColumnClausesqlalchemy.sql.expression ColumnElementsqlalchemy.sql.expression Executablesqlalchemy.sql.expression FunctionElementsqlalchemy.sql.expression UnaryExpressionsqlalchemy.sql.functions FunctionElementsqlalchemy.sql.functions GenericFunctionsqlalchemy.sql.naming convsqlalchemy.sql.schema Columnsqlalchemy.sql.schema SchemaItemsqlalchemy.sql.sqltypes NullTypesqlalchemy.sql.util ClauseAdaptersqlalchemy.sql.visitors traversesqlalchemy.types Booleansqlalchemy.types Datesqlalchemy.types DateTimesqlalchemy.types Enumsqlalchemy.types Floatsqlalchemy.types Integersqlalchemy.types Intervalsqlalchemy.types NULLTYPEsqlalchemy.types Stringsqlalchemy.types Textsqlalchemy.types Timesqlalchemy.types TypeEnginesqlalchemy.types UserDefinedTypesqlalchemy.types to_instancesqlalchemy.util OrderedDictsqlalchemy.util OrderedSetsqlalchemy.util set_creation_ordersqlalchemy.util symbolsqlalchemy.util topologicalsqlalchemy.util.langhelpers public_factorysqlalchemy.util.langhelpers symbol ...or view the full table of contents.

Full Stack Python

Full Stack Python is an open book that explains concepts in plain language and provides helpful resources for those topics.
Updates via Twitter & Facebook.

Matt Makai 2012-2022