summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2015-05-21 14:21:01 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2015-05-21 14:21:01 -0400
commit525cc6fe0247a76201c173e535d8309333461afc (patch)
tree45e4e2f10b6e89620360ff19467b975d486d41bb /lib/sqlalchemy/orm
parent164f2ad2f433b3d297df709d617a1fc421495921 (diff)
downloadsqlalchemy-525cc6fe0247a76201c173e535d8309333461afc.tar.gz
- Fixed regression in the :mod:`sqlalchemy.ext.mutable` extension
as a result of the bugfix for :ticket:`3167`, where attribute and validation events are no longer called within the flush process. The mutable extension was relying upon this behavior in the case where a column level Python-side default were responsible for generating the new value on INSERT or UPDATE, or when a value were fetched from the RETURNING clause for "eager defaults" mode. The new value would not be subject to any event when populated and the mutable extension could not establish proper coercion or history listening. A new event :meth:`.InstanceEvents.refresh_flush` is added which the mutable extension now makes use of for this use case. fixes #3427 - Added new event :meth:`.InstanceEvents.refresh_flush`, invoked when an INSERT or UPDATE level default value fetched via RETURNING or Python-side default is invoked within the flush process. This is to provide a hook that is no longer present as a result of :ticket:`3167`, where attribute and validation events are no longer called within the flush process. - Added a new semi-public method to :class:`.MutableBase` :meth:`.MutableBase._get_listen_keys`. Overriding this method is needed in the case where a :class:`.MutableBase` subclass needs events to propagate for attribute keys other than the key to which the mutable type is associated with, when intercepting the :meth:`.InstanceEvents.refresh` or :meth:`.InstanceEvents.refresh_flush` events. The current example of this is composites using :class:`.MutableComposite`.
Diffstat (limited to 'lib/sqlalchemy/orm')
-rw-r--r--lib/sqlalchemy/orm/events.py27
-rw-r--r--lib/sqlalchemy/orm/persistence.py12
2 files changed, 37 insertions, 2 deletions
diff --git a/lib/sqlalchemy/orm/events.py b/lib/sqlalchemy/orm/events.py
index 233cd66a6..801701be9 100644
--- a/lib/sqlalchemy/orm/events.py
+++ b/lib/sqlalchemy/orm/events.py
@@ -272,12 +272,35 @@ class InstanceEvents(event.Events):
object associated with the instance.
:param context: the :class:`.QueryContext` corresponding to the
current :class:`.Query` in progress.
- :param attrs: iterable collection of attribute names which
+ :param attrs: sequence of attribute names which
were populated, or None if all column-mapped, non-deferred
attributes were populated.
"""
+ def refresh_flush(self, target, flush_context, attrs):
+ """Receive an object instance after one or more attributes have
+ been refreshed within the persistence of the object.
+
+ This event is the same as :meth:`.InstanceEvents.refresh` except
+ it is invoked within the unit of work flush process, and the values
+ here typically come from the process of handling an INSERT or
+ UPDATE, such as via the RETURNING clause or from Python-side default
+ values.
+
+ .. versionadded:: 1.0.5
+
+ :param target: the mapped instance. If
+ the event is configured with ``raw=True``, this will
+ instead be the :class:`.InstanceState` state-management
+ object associated with the instance.
+ :param flush_context: Internal :class:`.UOWTransaction` object
+ which handles the details of the flush.
+ :param attrs: sequence of attribute names which
+ were populated.
+
+ """
+
def expire(self, target, attrs):
"""Receive an object instance after its attributes or some subset
have been expired.
@@ -289,7 +312,7 @@ class InstanceEvents(event.Events):
the event is configured with ``raw=True``, this will
instead be the :class:`.InstanceState` state-management
object associated with the instance.
- :param attrs: iterable collection of attribute
+ :param attrs: sequence of attribute
names which were expired, or None if all attributes were
expired.
diff --git a/lib/sqlalchemy/orm/persistence.py b/lib/sqlalchemy/orm/persistence.py
index ab2d54d90..b429aa4c1 100644
--- a/lib/sqlalchemy/orm/persistence.py
+++ b/lib/sqlalchemy/orm/persistence.py
@@ -950,6 +950,10 @@ def _postfetch(mapper, uowtransaction, table,
mapper.version_id_col in mapper._cols_by_table[table]:
prefetch_cols = list(prefetch_cols) + [mapper.version_id_col]
+ refresh_flush = bool(mapper.class_manager.dispatch.refresh_flush)
+ if refresh_flush:
+ load_evt_attrs = []
+
if returning_cols:
row = result.context.returned_defaults
if row is not None:
@@ -957,10 +961,18 @@ def _postfetch(mapper, uowtransaction, table,
if col.primary_key:
continue
dict_[mapper._columntoproperty[col].key] = row[col]
+ if refresh_flush:
+ load_evt_attrs.append(mapper._columntoproperty[col].key)
for c in prefetch_cols:
if c.key in params and c in mapper._columntoproperty:
dict_[mapper._columntoproperty[c].key] = params[c.key]
+ if refresh_flush:
+ load_evt_attrs.append(mapper._columntoproperty[c].key)
+
+ if refresh_flush and load_evt_attrs:
+ mapper.class_manager.dispatch.refresh_flush(
+ state, uowtransaction, load_evt_attrs)
if postfetch_cols:
state._expire_attributes(state.dict,