diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-03-11 12:39:00 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-03-11 12:39:00 -0400 |
| commit | b7169f66d7cac26713f0784713be916905f320de (patch) | |
| tree | 361d671d4eabf20e0b95f682de51c1d49f259ee2 /lib/sqlalchemy/orm | |
| parent | 009df6a3d041e517cc9efa74d3c87184357a5006 (diff) | |
| download | sqlalchemy-b7169f66d7cac26713f0784713be916905f320de.tar.gz | |
- A warning is emitted if the :meth:`.MapperEvents.before_configured`
or :meth:`.MapperEvents.after_configured` events are applied to a
specific mapper or mapped class, as the events are only invoked
for the :class:`.Mapper` target at the general level.
Diffstat (limited to 'lib/sqlalchemy/orm')
| -rw-r--r-- | lib/sqlalchemy/orm/events.py | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/lib/sqlalchemy/orm/events.py b/lib/sqlalchemy/orm/events.py index 52dcca232..b68475230 100644 --- a/lib/sqlalchemy/orm/events.py +++ b/lib/sqlalchemy/orm/events.py @@ -508,6 +508,13 @@ class MapperEvents(event.Events): target, identifier, fn = \ event_key.dispatch_target, event_key.identifier, event_key.fn + if identifier in ("before_configured", "after_configured") and \ + target is not mapperlib.Mapper: + util.warn( + "'before_configured' and 'after_configured' ORM events " + "only invoke with the mapper() function or Mapper class " + "as the target.") + if not raw or not retval: if not raw: meth = getattr(cls, identifier) @@ -590,11 +597,30 @@ class MapperEvents(event.Events): note is usually called automatically as mappings are first used. + This event can **only** be applied to the :class:`.Mapper` class + or :func:`.mapper` function, and not to individual mappings or + mapped classes. It is only invoked for all mappings as a whole:: + + from sqlalchemy.orm import mapper + + @event.listens_for(mapper, "before_configured") + def go(): + # ... + Theoretically this event is called once per application, but is actually called any time new mappers are to be affected by a :func:`.orm.configure_mappers` call. If new mappings are constructed after existing ones have - already been used, this event can be called again. + already been used, this event can be called again. To ensure + that a particular event is only called once and no further, the + ``once=True`` argument (new in 0.9.4) can be applied:: + + from sqlalchemy.orm import mapper + + @event.listens_for(mapper, "before_configured", once=True) + def go(): + # ... + .. versionadded:: 0.9.3 @@ -607,11 +633,29 @@ class MapperEvents(event.Events): note is usually called automatically as mappings are first used. + This event can **only** be applied to the :class:`.Mapper` class + or :func:`.mapper` function, and not to individual mappings or + mapped classes. It is only invoked for all mappings as a whole:: + + from sqlalchemy.orm import mapper + + @event.listens_for(mapper, "after_configured") + def go(): + # ... + Theoretically this event is called once per application, but is actually called any time new mappers have been affected by a :func:`.orm.configure_mappers` call. If new mappings are constructed after existing ones have - already been used, this event can be called again. + already been used, this event can be called again. To ensure + that a particular event is only called once and no further, the + ``once=True`` argument (new in 0.9.4) can be applied:: + + from sqlalchemy.orm import mapper + + @event.listens_for(mapper, "after_configured", once=True) + def go(): + # ... """ |
