From 9629838f7dceab972c70938b58b7ec1ff44739e2 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 5 Dec 2007 20:55:33 +0000 Subject: [PATCH] - fixed wrong varname in session exception throw - fixed vertical example to just use a scoped session --- examples/vertical/vertical.py | 9 ++++----- lib/sqlalchemy/orm/session.py | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/examples/vertical/vertical.py b/examples/vertical/vertical.py index e3b48c3369..225beeffe9 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 bb025a3ab3..5f8602105d 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): -- 2.47.2