diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-09-01 17:01:55 +0000 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-09-01 17:01:55 +0000 |
| commit | 35e2f6680b17dcf0f65b4e392d2504bfe8000efb (patch) | |
| tree | 6b155074e80a0972efad5e993ecf33ec497954bc /lib/sqlalchemy/attributes.py | |
| parent | 005603e2fb198c3f3328fa555e82a334f0f89d52 (diff) | |
| download | sqlalchemy-35e2f6680b17dcf0f65b4e392d2504bfe8000efb.tar.gz | |
futher fix to the "orphan state" idea. to avoid setting tons of
"hasparent" flags on objects as they are loaded, both from lazy and eager loads,
the "orphan" check now uses an "optimistic" flag to determine the result if no
"hasparent" flag is found for a particular relationship on an instance. if the
instance has an _instance_key and therefore was loaded from the database, it is
assumed to not be an orphan unless a "False" hasparent flag has been set. if the
instance does not have an _instance_key and is therefore transient/pending, it is
assumed to be an orphan unless a "True" hasparent flag has been set.
Diffstat (limited to 'lib/sqlalchemy/attributes.py')
| -rw-r--r-- | lib/sqlalchemy/attributes.py | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/lib/sqlalchemy/attributes.py b/lib/sqlalchemy/attributes.py index 3aada1951..8629d85a5 100644 --- a/lib/sqlalchemy/attributes.py +++ b/lib/sqlalchemy/attributes.py @@ -31,10 +31,13 @@ class InstrumentedAttribute(object): return self return self.get(obj) - def hasparent(self, item): - """returns True if the given item is attached to a parent object - via the attribute represented by this InstrumentedAttribute.""" - return item._state.get(('hasparent', id(self))) + def hasparent(self, item, optimistic=False): + """return True if the given item is attached to a parent object + via the attribute represented by this InstrumentedAttribute. + + optimistic indicates what we should return if the given item has no "hasparent" + record at all for the given attribute.""" + return item._state.get(('hasparent', id(self)), optimistic) def sethasparent(self, item, value): """sets a boolean flag on the given item corresponding to whether or not it is @@ -136,8 +139,12 @@ class InstrumentedAttribute(object): return InstrumentedAttribute.PASSIVE_NORESULT values = callable_() l = InstrumentedList(self, obj, self._adapt_list(values), init=False) - if self.trackparent and values is not None: - [self.sethasparent(v, True) for v in values if v is not None] + + # mark loaded instances with "hasparent" status. commented out + # because loaded objects use "optimistic" parent-checking + #if self.trackparent and values is not None: + # [self.sethasparent(v, True) for v in values if v is not None] + # if a callable was executed, then its part of the "committed state" # if any, so commit the newly loaded data orig = state.get('original', None) @@ -157,8 +164,12 @@ class InstrumentedAttribute(object): return InstrumentedAttribute.PASSIVE_NORESULT value = callable_() obj.__dict__[self.key] = value - if self.trackparent and value is not None: - self.sethasparent(value, True) + + # mark loaded instances with "hasparent" status. commented out + # because loaded objects use "optimistic" parent-checking + #if self.trackparent and value is not None: + # self.sethasparent(value, True) + # if a callable was executed, then its part of the "committed state" # if any, so commit the newly loaded data orig = state.get('original', None) |
