]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- fixed wrong varname in session exception throw
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 5 Dec 2007 20:55:33 +0000 (20:55 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 5 Dec 2007 20:55:33 +0000 (20:55 +0000)
- fixed vertical example to just use a scoped session

examples/vertical/vertical.py
lib/sqlalchemy/orm/session.py

index e3b48c3369ceaea3b004ad9705de1c4081599112..225beeffe92b11f657f752eaf856bb47612d03c8 100644 (file)
@@ -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'
index bb025a3ab3b51e9fb98e574cd5eecee20be480e4..5f8602105de98e803fa12cb94c1aa2684dad60dd 100644 (file)
@@ -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):