summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2007-12-05 20:55:33 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2007-12-05 20:55:33 +0000
commit9629838f7dceab972c70938b58b7ec1ff44739e2 (patch)
tree3d9ce7f2f4bd3d06e0e1f581057f5773ccb07a1f
parent9e4052dc8be2451d1c48bb059da150ce41ddc86f (diff)
downloadsqlalchemy-9629838f7dceab972c70938b58b7ec1ff44739e2.tar.gz
- fixed wrong varname in session exception throw
- fixed vertical example to just use a scoped session
-rw-r--r--examples/vertical/vertical.py9
-rw-r--r--lib/sqlalchemy/orm/session.py2
2 files changed, 5 insertions, 6 deletions
diff --git a/examples/vertical/vertical.py b/examples/vertical/vertical.py
index e3b48c336..225beeffe 100644
--- a/examples/vertical/vertical.py
+++ b/examples/vertical/vertical.py
@@ -10,6 +10,8 @@ import datetime
e = MetaData('sqlite://')
e.bind.echo = True
+Session = scoped_session(sessionmaker(transactional=True))
+
# this table represents Entity objects. each Entity gets a row in this table,
# with a primary key and a title.
entities = Table('entities', e,
@@ -84,10 +86,7 @@ class EntityValue(object):
the value to the underlying datatype of its EntityField."""
def __init__(self, key=None, value=None):
if key is not None:
- sess = create_session()
- self.field = sess.query(EntityField).get_by(name=key) or EntityField(key)
- # close the session, which will make a loaded EntityField a detached instance
- sess.close()
+ self.field = Session.query(EntityField).filter(EntityField.name==key).first() or EntityField(key)
if self.field.datatype is None:
if isinstance(value, int):
self.field.datatype = 'int'
@@ -123,7 +122,7 @@ mapper(Entity, entities, properties = {
# create two entities. the objects can be used about as regularly as
# any object can.
-session = create_session()
+session = Session()
entity = Entity()
entity.title = 'this is the first entity'
entity.name = 'this is the name'
diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py
index bb025a3ab..5f8602105 100644
--- a/lib/sqlalchemy/orm/session.py
+++ b/lib/sqlalchemy/orm/session.py
@@ -993,7 +993,7 @@ class Session(object):
if not hasattr(instance, '_instance_key'):
raise exceptions.InvalidRequestError("Instance '%s' is not persisted" % mapperutil.instance_str(instance))
elif self.identity_map.get(instance._instance_key, instance) is not instance:
- raise exceptions.InvalidRequestError("Could not update instance '%s', identity key %s; a different instance with the same identity key already exists in this session." % (mapperutil.instance_str(obj), obj._instance_key))
+ raise exceptions.InvalidRequestError("Could not update instance '%s', identity key %s; a different instance with the same identity key already exists in this session." % (mapperutil.instance_str(instance), instance._instance_key))
self._attach(instance)
def _save_or_update_impl(self, instance, entity_name=None):