From: Mike Bayer Date: Sat, 25 Mar 2006 19:34:34 +0000 (+0000) Subject: dev X-Git-Tag: rel_0_1_5~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ee537dccb2056ef3ba448c683ef874480bae8a26;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git dev --- diff --git a/doc/build/content/unitofwork.myt b/doc/build/content/unitofwork.myt index fd2171c03d..d9d160ccdd 100644 --- a/doc/build/content/unitofwork.myt +++ b/doc/build/content/unitofwork.myt @@ -54,6 +54,50 @@ [<__main__.User object at 0x712630>, <__main__.Address object at 0x712a70>] """ %> + +

The identity of each object instance is available via the _instance_key property attached to each object instance, and is a tuple consisting of the object's class and an additional tuple of primary key values, in the order that they appear within the table definition:

+ <&|formatting.myt:code&> + >>> obj._instance_key + (, (7,)) + + +

+ At the moment that an object is assigned this key, it is also added to the current thread's unit-of-work's identity map. +

+ +

The get() method on a mapper, which retrieves an object based on primary key identity, also checks in the current identity map first to save a database round-trip if possible. In the case of an object lazy-loading a single child object, the get() method is used as well, so scalar-based lazy loads may in some cases not query the database; this is particularly important for backreference relationships as it can save a lot of queries.

+ +

Methods on mappers and the objectstore module, which are relevant to identity include the following:

+ <&|formatting.myt:code&> + # assume 'm' is a mapper + m = mapper(User, users) + + # get the identity key corresponding to a primary key + key = m.identity_key(7) + + # for composite key, list out the values in the order they + # appear in the table + key = m.identity_key(12, 'rev2') + + # get the identity key given a primary key + # value as a tuple and a class + key = objectstore.get_id_key((12, 'rev2'), User) + + # get the identity key for an object, whether or not it actually + # has one attached to it (m is the mapper for obj's class) + key = m.instance_key(obj) + + # is this key in the current identity map? + session.has_key(key) + + # is this object in the current identity map? + session.has_instance(obj) + + # get this object from the current identity map based on + # singular/composite primary key, or if not go + # and load from the database + obj = m.get(12, 'rev2') + @@ -156,7 +200,7 @@

This second form of commit should be used more carefully as it will not necessarily locate other dependent objects within the session, whose database representation may have foreign constraint relationships with the objects being operated upon.

<&|doclib.myt:item, name="whatis", description="What Commit is, and Isn't" &> -

The purpose of the Commit operation is to instruct the Unit of Work to analyze its lists of modified objects, assemble them into a dependency graph, fire off the appopriate INSERT, UPDATE, and DELETE statements via the mappers related to those objects, and update the identifying object attributes that correspond directly to database columns. And thats it. This means, it is not going to change anything about your objects as they exist in memory, with the exception of synchronizing the identifier attributes on saved and updated objects as they correspond directly to newly inserted or updated rows, which typically include only primary key and foreign key attributes that in most cases are integers. A brief list of what will not happen includes:

+

The purpose of the Commit operation is to instruct the Unit of Work to analyze its lists of modified objects, assemble them into a dependency graph, fire off the appopriate INSERT, UPDATE, and DELETE statements via the mappers related to those objects, and to synchronize column-based object attributes that correspond directly to updated/inserted database columns. And thats it. It does not affect any relation-based object attributes, that is attributes that reference other objects or lists of other objects, in any way. A brief list of what will not happen includes: