From 3be8746527f17d18a56b773110bbb640c43e3d11 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 23 Jan 2011 17:08:57 -0500 Subject: [PATCH] add example usage for shard_id in context feature [ticket:2031] --- examples/sharding/attribute_shard.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/examples/sharding/attribute_shard.py b/examples/sharding/attribute_shard.py index 9f1157c32b..b36b8a01db 100644 --- a/examples/sharding/attribute_shard.py +++ b/examples/sharding/attribute_shard.py @@ -1,7 +1,7 @@ # step 1. imports from sqlalchemy import (create_engine, MetaData, Table, Column, Integer, - String, ForeignKey, Float, DateTime) + String, ForeignKey, Float, DateTime, event) from sqlalchemy.orm import sessionmaker, mapper, relationship from sqlalchemy.ext.horizontal_shard import ShardedSession from sqlalchemy.sql import operators, visitors @@ -233,6 +233,15 @@ mapper(WeatherLocation, weather_locations, properties={ mapper(Report, weather_reports) +# step 8 (optional), events. The "shard_id" is placed +# in the QueryContext where it can be intercepted and associated +# with objects, if needed. + +def add_shard_id(instance, ctx): + instance.shard_id = ctx.attributes["shard_id"] + +event.listen(WeatherLocation, "load", add_shard_id) +event.listen(Report, "load", add_shard_id) # save and load objects! @@ -253,7 +262,11 @@ for c in [tokyo, newyork, toronto, london, dublin, brasilia, quito]: sess.add(c) sess.commit() -t = sess.query(WeatherLocation).get(tokyo.id) +tokyo_id = tokyo.id + +sess.close() + +t = sess.query(WeatherLocation).get(tokyo_id) assert t.city == tokyo.city assert t.reports[0].temperature == 80.0 -- 2.47.3