diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-01-23 17:08:57 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-01-23 17:08:57 -0500 |
| commit | 3be8746527f17d18a56b773110bbb640c43e3d11 (patch) | |
| tree | 8e21f3557d2922c00168aa2b64d3a7e993084305 /examples | |
| parent | de725a0a4daea48fa4d6ffff892620e93c358b5e (diff) | |
| download | sqlalchemy-3be8746527f17d18a56b773110bbb640c43e3d11.tar.gz | |
add example usage for shard_id in context feature [ticket:2031]
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/sharding/attribute_shard.py | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/examples/sharding/attribute_shard.py b/examples/sharding/attribute_shard.py index 9f1157c32..b36b8a01d 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 |
