ORM Examples

    Additional SQLAlchemy examples, some user contributed, are available on the wiki at https://www.sqlalchemy.org/trac/wiki/UsageRecipes.

    An example of a dictionary-of-dictionaries structure mapped using an adjacency list model.

    E.g.:

    Listing of files:

    Associations

    Examples illustrating the usage of the “association object” pattern, where an intermediary class mediates the relationship between two classes that are associated in a many-to-many pattern.

    Listing of files:

    • - Same example as basic_association, adding in usage of sqlalchemy.ext.associationproxy to make explicit references to OrderItem optional.

    • - Illustrate a many-to-many relationship between an “Order” and a collection of “Item” objects, associating a purchase price with each via an association object called “OrderItem”

    • dict_of_sets_with_default.py - An advanced association proxy example which illustrates nesting of association proxies to produce multi-level Python collections, in this case a dictionary with string keys and sets of integers as values, which conceal the underlying mapped classes.

    Asyncio Integration

    Examples illustrating the asyncio engine feature of SQLAlchemy.

    Listing of files:

    • async_orm.py - Illustrates use of the sqlalchemy.ext.asyncio.AsyncSession object for asynchronous ORM use.

    • - Illustrates how to run many statements concurrently using asyncio.gather() along many asyncio database connections, merging ORM results into a single AsyncSession.

    • basic.py - Illustrates the asyncio engine / connection interface.

    • - Illustrates use of the sqlalchemy.ext.asyncio.AsyncSession object for asynchronous ORM use, including the optional run_sync() method.

    Directed Graphs

    An example of persistence for a directed graph structure. The graph is stored as a collection of edges, each referencing both a “lower” and an “upper” node in a table of nodes. Basic persistence and querying for lower- and upper- neighbors are illustrated:

    1. n2 = Node(2)
    2. n5 = Node(5)
    3. n2.add_neighbor(n5)
    4. print(n2.higher_neighbors())

    Listing of files:

    Dynamic Relations as Dictionaries

    Illustrates how to place a dictionary-like facade on top of a “dynamic” relation, so that dictionary operations (assuming simple string keys) can operate upon a large collection without loading the full collection at once.

    Listing of files:

    Illustrates various methods of associating multiple types of parents with a particular child object.

    The examples all use the declarative extension along with declarative mixins. Each one presents the identical use case at the end - two classes, Customer and Supplier, both subclassing the HasAddresses mixin, which ensures that the parent class is provided with an addresses collection which contains Address objects.

    The and generic_fk.py scripts are modernized versions of recipes presented in the 2007 blog post .

    Listing of files:

    • table_per_association.py - Illustrates a mixin which provides a generic association via a individually generated association tables for each parent class. The associated objects themselves are persisted in a single table shared among all parents.

    • - Illustrates a generic association which persists association objects within individual tables, each one generated to persist those objects on behalf of a particular parent class.

    • discriminator_on_association.py - Illustrates a mixin which provides a generic association using a single target table and a single association table, referred to by all parent tables. The association table contains a “discriminator” column which determines what type of parent object associates to each particular row in the association table.

    • - Illustrates a so-called “generic foreign key”, in a similar fashion to that of popular frameworks such as Django, ROR, etc. This approach bypasses standard referential integrity practices, in that the “foreign key” column is not actually constrained to refer to any particular table; instead, in-application logic is used to determine which table is referenced.

    Materialized Paths

    Illustrates the “materialized paths” pattern for hierarchical data using the SQLAlchemy ORM.

    Listing of files:

    • - Illustrates the “materialized paths” pattern.

    Nested Sets

    Illustrates a rudimentary way to implement the “nested sets” pattern for hierarchical data using the SQLAlchemy ORM.

    Listing of files:

    • - Celko’s “Nested Sets” Tree Structure.

    Performance

    A performance profiling suite for a variety of SQLAlchemy use cases.

    Each suite focuses on a specific use case with a particular performance profile and associated implications:

    • bulk inserts

    • individual inserts, with or without transactions

    • fetching large numbers of rows

    • running lots of short queries

    All suites include a variety of use patterns illustrating both Core and ORM use, and are generally sorted in order of performance from worst to greatest, inversely based on amount of functionality provided by SQLAlchemy, greatest to least (these two things generally correspond perfectly).

    A command line tool is presented at the package level which allows individual suites to be run:

    1. $ python -m examples.performance --help
    2. usage: python -m examples.performance [-h] [--test TEST] [--dburl DBURL]
    3. [--num NUM] [--profile] [--dump]
    4. [--echo]
    5. {bulk_inserts,large_resultsets,single_inserts}
    6. positional arguments:
    7. {bulk_inserts,large_resultsets,single_inserts}
    8. suite to run
    9. optional arguments:
    10. -h, --help show this help message and exit
    11. --test TEST run specific test name
    12. --dburl DBURL database URL, default sqlite:///profile.db
    13. --num NUM Number of iterations/items/etc for tests;
    14. default is module-specific
    15. --profile run profiling and dump call counts
    16. --dump dump full call profile (implies --profile)
    17. --echo Echo SQL output

    An example run looks like:

    1. $ python -m examples.performance bulk_inserts

    Or with options:

    1. $ python -m examples.performance bulk_inserts \
    2. --dburl mysql+mysqldb://scott:tiger@localhost/test \
    3. --profile --num 1000

    See also

    File Listing

    Listing of files:

    • - In this series of tests, we are looking at time to load a large number of very small and simple rows.

    • bulk_inserts.py - This series of tests illustrates different ways to INSERT a large number of rows in bulk.

    • - This series of tests illustrates different ways to SELECT a single record by primary key

    • single_inserts.py - In this series of tests, we’re looking at a method that inserts a row within a distinct transaction, and afterwards returns to essentially a “closed” state. This would be analogous to an API call that starts up a database connection, inserts the row, commits and closes.

    • - Allows the examples/performance package to be run as a script.

    Running all tests with time

    This is the default form of run:

    1. $ python -m examples.performance single_inserts
    2. Tests to run: test_orm_commit, test_bulk_save,
    3. test_bulk_insert_dictionaries, test_core,
    4. test_core_query_caching, test_dbapi_raw_w_connect,
    5. test_dbapi_raw_w_pool
    6. test_orm_commit : Individual INSERT/COMMIT pairs via the
    7. ORM (10000 iterations); total time 13.690218 sec
    8. test_bulk_save : Individual INSERT/COMMIT pairs using
    9. test_bulk_insert_dictionaries : Individual INSERT/COMMIT pairs using
    10. the "bulk" API with dictionaries (10000 iterations);
    11. total time 10.814626 sec
    12. test_core : Individual INSERT/COMMIT pairs using Core.
    13. (10000 iterations); total time 9.665620 sec
    14. test_core_query_caching : Individual INSERT/COMMIT pairs using Core
    15. with query caching (10000 iterations); total time 9.209010 sec
    16. test_dbapi_raw_w_connect : Individual INSERT/COMMIT pairs w/ DBAPI +
    17. connection each time (10000 iterations); total time 9.551103 sec
    18. test_dbapi_raw_w_pool : Individual INSERT/COMMIT pairs w/ DBAPI +
    19. connection pool (10000 iterations); total time 8.001813 sec

    Dumping Profiles for Individual Tests

    A Python profile output can be dumped for all tests, or more commonly individual tests:

    Writing your Own Suites

    The profiler suite system is extensible, and can be applied to your own set of tests. This is a valuable technique to use in deciding upon the proper approach for some performance-critical set of routines. For example, if we wanted to profile the difference between several kinds of loading, we can create a file test_loads.py, with the following content:

    1. from examples.performance import Profiler
    2. from sqlalchemy import Integer, Column, create_engine, ForeignKey
    3. from sqlalchemy.orm import relationship, joinedload, subqueryload, Session
    4. from sqlalchemy.ext.declarative import declarative_base
    5. Base = declarative_base()
    6. engine = None
    7. session = None
    8. class Parent(Base):
    9. __tablename__ = 'parent'
    10. id = Column(Integer, primary_key=True)
    11. children = relationship("Child")
    12. class Child(Base):
    13. __tablename__ = 'child'
    14. id = Column(Integer, primary_key=True)
    15. parent_id = Column(Integer, ForeignKey('parent.id'))
    16. # Init with name of file, default number of items
    17. Profiler.init("test_loads", 1000)
    18. @Profiler.setup_once
    19. def setup_once(dburl, echo, num):
    20. "setup once. create an engine, insert fixture data"
    21. global engine
    22. engine = create_engine(dburl, echo=echo)
    23. Base.metadata.drop_all(engine)
    24. Base.metadata.create_all(engine)
    25. sess = Session(engine)
    26. sess.add_all([
    27. Parent(children=[Child() for j in range(100)])
    28. for i in range(num)
    29. ])
    30. sess.commit()
    31. @Profiler.setup
    32. def setup(dburl, echo, num):
    33. "setup per test. create a new Session."
    34. global session
    35. session = Session(engine)
    36. # pre-connect so this part isn't profiled (if we choose)
    37. session.connection()
    38. @Profiler.profile
    39. def test_lazyload(n):
    40. "load everything, no eager loading."
    41. for parent in session.query(Parent):
    42. parent.children
    43. @Profiler.profile
    44. def test_joinedload(n):
    45. "load everything, joined eager loading."
    46. for parent in session.query(Parent).options(joinedload("children")):
    47. parent.children
    48. @Profiler.profile
    49. def test_subqueryload(n):
    50. "load everything, subquery eager loading."
    51. for parent in session.query(Parent).options(subqueryload("children")):
    52. parent.children
    53. if __name__ == '__main__':
    54. Profiler.main()

    We can run our new script directly:

    1. Running setup once...
    2. Tests to run: test_lazyload, test_joinedload, test_subqueryload
    3. test_lazyload : load everything, no eager loading. (1000 iterations); total time 11.971159 sec
    4. test_subqueryload : load everything, subquery eager loading. (1000 iterations); total time 2.977696 sec

    Space Invaders

    A Space Invaders game using SQLite as the state machine.

    Originally developed in 2012. Adapted to work in Python 3.

    Runs in a textual console using ASCII art.

    To run:

    1. python -m examples.space_invaders.space_invaders

    While it runs, watch the SQL output in the log:

    1. tail -f space_invaders.log

    enjoy!

    Listing of files:

    Versioning with a History Table

    Illustrates an extension which creates version tables for entities and stores records for each change. The given extensions generate an anonymous “history” class which represents historical versions of the target object.

    Compare to the examples which write updates as new rows in the same table, without using a separate history table.

    Usage is illustrated via a unit test module test_versioning.py, which can be run via pytest:

    1. # assume SQLAlchemy is installed where pytest is
    2. cd examples/versioned_history
    3. pytest test_versioning.py

    A fragment of example usage, using declarative:

    The Versioned mixin is designed to work with declarative. To use the extension with classical mappers, the _history_mapper function can be applied:

    1. from history_meta import _history_mapper
    2. m = mapper(SomeClass, sometable)
    3. _history_mapper(m)
    4. SomeHistoryClass = SomeClass.__history_mapper__.class_

    The versioning example also integrates with the ORM optimistic concurrency feature documented at Configuring a Version Counter. To enable this feature, set the flag Versioned.use_mapper_versioning to True:

    1. class SomeClass(Versioned, Base):
    2. __tablename__ = 'sometable'
    3. use_mapper_versioning = True
    4. id = Column(Integer, primary_key=True)
    5. name = Column(String(50))
    6. def __eq__(self, other):
    7. assert type(other) is SomeClass and other.id == self.id

    Above, if two instance of SomeClass with the same version identifier are updated and sent to the database for UPDATE concurrently, if the database isolation level allows the two UPDATE statements to proceed, one will fail because it no longer is against the last known version identifier.

    Listing of files:

    • - Unit tests illustrating usage of the history_meta.py module functions.

    • history_meta.py - Versioned mixin class and other utilities.

    Versioning using Temporal Rows

    Several examples that illustrate the technique of intercepting changes that would be first interpreted as an UPDATE on a row, and instead turning it into an INSERT of a new row, leaving the previous row intact as a historical version.

    Compare to the Versioning with a History Table example which writes a history row to a separate history table.

    Listing of files:

    • - Illustrates a method to intercept changes on objects, turning an UPDATE statement on a single row into an INSERT statement, so that a new row is inserted with the new data, keeping the old row intact.

    • versioned_rows_w_versionid.py - Illustrates a method to intercept changes on objects, turning an UPDATE statement on a single row into an INSERT statement, so that a new row is inserted with the new data, keeping the old row intact.

    • - A variant of the versioned_rows example built around the concept of a “vertical table” structure, like those illustrated in Vertical Attribute Mapping examples.

    • - Illustrates the same UPDATE into INSERT technique of versioned_rows.py, but also emits an UPDATE on the old row to affect a change in timestamp. Also includes a SessionEvents.do_orm_execute() hook to limit queries to only the most recent version.

    Vertical Attribute Mapping

    Illustrates “vertical table” mappings.

    A “vertical table” refers to a technique where individual attributes of an object are stored as distinct rows in a table. The “vertical table” technique is used to persist objects which can have a varied set of attributes, at the expense of simple query control and brevity. It is commonly found in content/document management systems in order to represent user-created structures flexibly.

    Two variants on the approach are given. In the second, each row references a “datatype” which contains information about the type of information stored in the attribute, such as integer, string, or date.

    Example:

    1. shrew = Animal(u'shrew')
    2. shrew[u'cuteness'] = 5
    3. shrew[u'weasel-like'] = False
    4. shrew[u'poisonous'] = True
    5. session.add(shrew)
    6. session.flush()
    7. q = (session.query(Animal).
    8. filter(Animal.facts.any(
    9. and_(AnimalFact.key == u'weasel-like',
    10. AnimalFact.value == True))))
    11. print('weasel-like animals', q.all())

    Listing of files:

    • dictlike-polymorphic.py - Mapping a polymorphic-valued vertical table as a dictionary.

    • - Mapping a vertical table as a dictionary.

    Basic Inheritance Mappings

    Working examples of single-table, joined-table, and concrete-table inheritance as described in Mapping Class Inheritance Hierarchies.

    Listing of files:

    • - Joined-table (table-per-subclass) inheritance example.

    • single.py - Single-table (table-per-hierarchy) inheritance example.

    Attribute Instrumentation

    Examples illustrating modifications to SQLAlchemy’s attribute management system.

    Listing of files:

    • - Illustrates how to attach events to all instrumented attributes and listen for change events.

    • active_column_defaults.py - Illustrates use of the event, in conjunction with Core column defaults to provide ORM objects that automatically produce the default value when an un-set attribute is accessed.

    • custom_management.py - Illustrates customized class instrumentation, using the extension package.

    Horizontal Sharding

    A basic example of using the SQLAlchemy Sharding API. Sharding refers to horizontally scaling data across multiple databases.

    The basic components of a “sharded” mapping are:

    • multiple instances, each assigned a “shard id”. These Engine instances may refer to different databases, or different schemas / accounts within the same database, or they can even be differentiated only by options that will cause them to access different schemas or tables when used.

    • a function which can return a single shard id, given an instance to be saved; this is called “shard_chooser”

    • a function which can return a list of shard ids which apply to a particular instance identifier; this is called “id_chooser”.If it returns all shard ids, all shards will be searched.

    • a function which can return a list of shard ids to try, given a particular Query (“query_chooser”). If it returns all shard ids, all shards will be queried and the results joined together.

    In these examples, different kinds of shards are used against the same basic example which accommodates weather data on a per-continent basis. We provide example shard_chooser, id_chooser and query_chooser functions. The query_chooser illustrates inspection of the SQL expression element in order to attempt to determine a single shard being requested.

    The construction of generic sharding routines is an ambitious approach to the issue of organizing instances among multiple databases. For a more plain-spoken alternative, the “distinct entity” approach is a simple method of assigning objects to different tables (and potentially database nodes) in an explicit way - described on the wiki at .

    Listing of files:

    • separate_databases.py - Illustrates sharding using distinct SQLite databases.

    • - Illustrates sharding using a single SQLite database, that will however have multiple tables using a naming convention.

    • separate_schema_translates.py - Illustrates sharding using a single database with multiple schemas, where a different “schema_translates_map” can be used for each shard.

    • - Illustrates sharding API used with asyncio.

    Recipes which illustrate augmentation of ORM SELECT behavior as used by Session.execute() with use of select(), as well as the Query object.

    Examples include demonstrations of the option as well as the SessionEvents.do_orm_execute() hook.

    As of SQLAlchemy 1.4, the construct is unified with the Select construct, so that these two objects are mostly the same.

    Listing of files:

    • - Illustrates a custom per-query criteria that will be applied to selected entities.

    • filter_public.py - Illustrates a global criteria applied to entities of a particular type.

    Dogpile Caching

    Illustrates how to embed dogpile.cache functionality with ORM queries, allowing full cache control as well as the ability to pull “lazy loaded” attributes from long term cache.

    In this demo, the following techniques are illustrated:

    • Using the event hook

    • Basic technique of circumventing Session.execute() to pull from a custom cache source instead of the database.

    • Rudimental caching with dogpile.cache, using “regions” which allow global control over a fixed set of configurations.

    • Using custom UserDefinedOption objects to configure options in a statement object.

    See also

    - includes a general example of the technique presented here.

    E.g.:

    1. # query for Person objects, specifying cache
    2. stmt = select(Person).options(FromCache("default"))
    3. # specify that each Person's "addresses" collection comes from
    4. # cache too
    5. stmt = stmt.options(RelationshipCache(Person.addresses, "default"))
    6. # execute and results
    7. result = session.execute(stmt)
    8. print(result.scalars().all())

    To run, both SQLAlchemy and dogpile.cache must be installed or on the current PYTHONPATH. The demo will create a local directory for datafiles, insert initial data, and run. Running the demo a second time will utilize the cache files already present, and exactly one SQL statement against two tables will be emitted - the displayed result however will utilize dozens of lazyloads that all pull from cache.

    The demo scripts themselves, in order of complexity, are run as Python modules so that relative imports work:

    1. python -m examples.dogpile_caching.helloworld
    2. python -m examples.dogpile_caching.relationship_caching
    3. python -m examples.dogpile_caching.advanced

    Listing of files:

    • environment.py - Establish data / cache file paths, and configurations, bootstrap fixture data if necessary.

    • - Represent functions and classes which allow the usage of Dogpile caching with SQLAlchemy. Introduces a query option called FromCache.

    • model.py - The datamodel, which represents Person that has multiple Address objects, each with PostalCode, City, Country.

    • - Installs some sample data. Here we have a handful of postal codes for a few US/Canadian cities. Then, 100 Person records are installed, each with a randomly selected postal code.

    • helloworld.py - Illustrate how to load some data, and cache the results.

    • - Illustrates how to add cache options on relationship endpoints, so that lazyloads load from cache.

    • advanced.py - Illustrate usage of Query combined with the FromCache option, including front-end loading, cache invalidation and collection caching.