Core Events

This section describes the event interfaces provided inSQLAlchemy Core.For an introduction to the event listening API, see Events.ORM events are described in ORM Events.

  • class sqlalchemy.event.base.Events
  • Define event listening functions for a particular target type.

Connection Pool Events

Available events for Pool.

The methods here define the name of an event as wellas the names of members that are passed to listenerfunctions.

e.g.:

  1. from sqlalchemy import event
  2.  
  3. def my_on_checkout(dbapi_conn, connection_rec, connection_proxy):
  4. "handle an on checkout event"
  5.  
  6. event.listen(Pool, 'checkout', my_on_checkout)

In addition to accepting the Pool class andPool instances, PoolEvents also acceptsEngine objects and the Engine class astargets, which will be resolved to the .pool attribute of thegiven engine or the Pool class:

  1. engine = create_engine("postgresql://scott:tiger@localhost/test")
  2.  
  3. # will associate with engine.pool
  4. event.listen(engine, 'checkout', my_on_checkout)
  • checkin(dbapi_connection, connection_record)
  • Called when a connection returns to the pool.

Example argument forms:

  1. from sqlalchemy import event
  2.  
  3. # standard decorator style
  4. @event.listens_for(SomeEngineOrPool, 'checkin')
  5. def receive_checkin(dbapi_connection, connection_record):
  6. "listen for the 'checkin' event"
  7.  
  8. # ... (event handling logic) ...

Note that the connection may be closed, and may be None if theconnection has been invalidated. checkin will not be calledfor detached connections. (They do not return to the pool.)

  1. - Parameters
  2. -
  3. -

dbapi_connection – a DBAPI connection.

  1. -

connection_record – the _ConnectionRecord managing theDBAPI connection.

  • checkout(dbapi_connection, connection_record, connection_proxy)
  • Called when a connection is retrieved from the Pool.

Example argument forms:

  1. from sqlalchemy import event
  2.  
  3. # standard decorator style
  4. @event.listens_for(SomeEngineOrPool, 'checkout')
  5. def receive_checkout(dbapi_connection, connection_record, connection_proxy):
  6. "listen for the 'checkout' event"
  7.  
  8. # ... (event handling logic) ...
  1. - Parameters
  2. -
  3. -

dbapi_connection – a DBAPI connection.

  1. -

connection_record – the _ConnectionRecord managing theDBAPI connection.

  1. -

connection_proxy – the _ConnectionFairy object whichwill proxy the public interface of the DBAPI connection for thelifespan of the checkout.

If you raise a DisconnectionError, the currentconnection will be disposed and a fresh connection retrieved.Processing of all checkout listeners will abort and restartusing the new connection.

See also

ConnectionEvents.engine_connect() - a similar eventwhich occurs upon creation of a new Connection.

  • close(dbapi_connection, connection_record)
  • Called when a DBAPI connection is closed.

Example argument forms:

  1. from sqlalchemy import event
  2.  
  3. # standard decorator style
  4. @event.listens_for(SomeEngineOrPool, 'close')
  5. def receive_close(dbapi_connection, connection_record):
  6. "listen for the 'close' event"
  7.  
  8. # ... (event handling logic) ...

The event is emitted before the close occurs.

The close of a connection can fail; typically this is becausethe connection is already closed. If the close operation fails,the connection is discarded.

The close() event corresponds to a connection that’s stillassociated with the pool. To intercept close events for detachedconnections use close_detached().

New in version 1.1.

  • closedetached(_dbapi_connection)
  • Called when a detached DBAPI connection is closed.

Example argument forms:

  1. from sqlalchemy import event
  2.  
  3. # standard decorator style
  4. @event.listens_for(SomeEngineOrPool, 'close_detached')
  5. def receive_close_detached(dbapi_connection):
  6. "listen for the 'close_detached' event"
  7.  
  8. # ... (event handling logic) ...

The event is emitted before the close occurs.

The close of a connection can fail; typically this is becausethe connection is already closed. If the close operation fails,the connection is discarded.

New in version 1.1.

  • connect(dbapi_connection, connection_record)
  • Called at the moment a particular DBAPI connection is firstcreated for a given Pool.

Example argument forms:

  1. from sqlalchemy import event
  2.  
  3. # standard decorator style
  4. @event.listens_for(SomeEngineOrPool, 'connect')
  5. def receive_connect(dbapi_connection, connection_record):
  6. "listen for the 'connect' event"
  7.  
  8. # ... (event handling logic) ...

This event allows one to capture the point directly after whichthe DBAPI module-level .connect() method has been used in orderto produce a new DBAPI connection.

  1. - Parameters
  2. -
  3. -

dbapi_connection – a DBAPI connection.

  1. -

connection_record – the _ConnectionRecord managing theDBAPI connection.

  • detach(dbapi_connection, connection_record)
  • Called when a DBAPI connection is “detached” from a pool.

Example argument forms:

  1. from sqlalchemy import event
  2.  
  3. # standard decorator style
  4. @event.listens_for(SomeEngineOrPool, 'detach')
  5. def receive_detach(dbapi_connection, connection_record):
  6. "listen for the 'detach' event"
  7.  
  8. # ... (event handling logic) ...

This event is emitted after the detach occurs. The connectionis no longer associated with the given connection record.

New in version 1.1.

  • firstconnect(_dbapi_connection, connection_record)
  • Called exactly once for the first time a DBAPI connection ischecked out from a particular Pool.

Example argument forms:

  1. from sqlalchemy import event
  2.  
  3. # standard decorator style
  4. @event.listens_for(SomeEngineOrPool, 'first_connect')
  5. def receive_first_connect(dbapi_connection, connection_record):
  6. "listen for the 'first_connect' event"
  7.  
  8. # ... (event handling logic) ...

The rationale for PoolEvents.first_connect() is to determineinformation about a particular series of database connections basedon the settings used for all connections. Since a particularPool refers to a single “creator” function (which in termsof a Engine refers to the URL and connection options used),it is typically valid to make observations about a single connectionthat can be safely assumed to be valid about all subsequentconnections, such as the database version, the server and clientencoding settings, collation settings, and many others.

  1. - Parameters
  2. -
  3. -

dbapi_connection – a DBAPI connection.

  1. -

connection_record – the _ConnectionRecord managing theDBAPI connection.

  • invalidate(dbapi_connection, connection_record, exception)
  • Called when a DBAPI connection is to be “invalidated”.

Example argument forms:

  1. from sqlalchemy import event
  2.  
  3. # standard decorator style
  4. @event.listens_for(SomeEngineOrPool, 'invalidate')
  5. def receive_invalidate(dbapi_connection, connection_record, exception):
  6. "listen for the 'invalidate' event"
  7.  
  8. # ... (event handling logic) ...

This event is called any time the _ConnectionRecord.invalidate()method is invoked, either from API usage or via “auto-invalidation”,without the soft flag.

The event occurs before a final attempt to call .close() on theconnection occurs.

  1. - Parameters
  2. -
  3. -

dbapi_connection – a DBAPI connection.

  1. -

connection_record – the _ConnectionRecord managing theDBAPI connection.

  1. -

exception – the exception object corresponding to the reasonfor this invalidation, if any. May be None.

New in version 0.9.2: Added support for connection invalidationlistening.

See also

More on Invalidation

  • reset(dbapi_connection, connection_record)
  • Called before the “reset” action occurs for a pooled connection.

Example argument forms:

  1. from sqlalchemy import event
  2.  
  3. # standard decorator style
  4. @event.listens_for(SomeEngineOrPool, 'reset')
  5. def receive_reset(dbapi_connection, connection_record):
  6. "listen for the 'reset' event"
  7.  
  8. # ... (event handling logic) ...

This event representswhen the rollback() method is called on the DBAPI connectionbefore it is returned to the pool. The behavior of “reset” canbe controlled, including disabled, using the reset_on_returnpool argument.

The PoolEvents.reset() event is usually followed by thePoolEvents.checkin() event is called, except in thosecases where the connection is discarded immediately after reset.

  1. - Parameters
  2. -
  3. -

dbapi_connection – a DBAPI connection.

  1. -

connection_record – the _ConnectionRecord managing theDBAPI connection.

See also

ConnectionEvents.rollback()

ConnectionEvents.commit()

  • softinvalidate(_dbapi_connection, connection_record, exception)
  • Called when a DBAPI connection is to be “soft invalidated”.

Example argument forms:

  1. from sqlalchemy import event
  2.  
  3. # standard decorator style
  4. @event.listens_for(SomeEngineOrPool, 'soft_invalidate')
  5. def receive_soft_invalidate(dbapi_connection, connection_record, exception):
  6. "listen for the 'soft_invalidate' event"
  7.  
  8. # ... (event handling logic) ...

This event is called any time the _ConnectionRecord.invalidate()method is invoked with the soft flag.

Soft invalidation refers to when the connection record that tracksthis connection will force a reconnect after the current connectionis checked in. It does not actively close the dbapi_connectionat the point at which it is called.

New in version 1.0.3.

SQL Execution and Connection Events

Available events for Connectable, which includesConnection and Engine.

The methods here define the name of an event as well as the names ofmembers that are passed to listener functions.

An event listener can be associated with any Connectableclass or instance, such as an Engine, e.g.:

  1. from sqlalchemy import event, create_engine
  2.  
  3. def before_cursor_execute(conn, cursor, statement, parameters, context,
  4. executemany):
  5. log.info("Received statement: %s", statement)
  6.  
  7. engine = create_engine('postgresql://scott:tiger@localhost/test')
  8. event.listen(engine, "before_cursor_execute", before_cursor_execute)

or with a specific Connection:

  1. with engine.begin() as conn:
  2. @event.listens_for(conn, 'before_cursor_execute')
  3. def before_cursor_execute(conn, cursor, statement, parameters,
  4. context, executemany):
  5. log.info("Received statement: %s", statement)

When the methods are called with a statement parameter, such as inafter_cursor_execute(), before_cursor_execute() anddbapi_error(), the statement is the exact SQL string that wasprepared for transmission to the DBAPI cursor in the connection’sDialect.

The before_execute() and before_cursor_execute()events can also be established with the retval=True flag, whichallows modification of the statement and parameters to be sentto the database. The before_cursor_execute() event isparticularly useful here to add ad-hoc string transformations, suchas comments, to all executions:

  1. from sqlalchemy.engine import Engine
  2. from sqlalchemy import event
  3.  
  4. @event.listens_for(Engine, "before_cursor_execute", retval=True)
  5. def comment_sql_calls(conn, cursor, statement, parameters,
  6. context, executemany):
  7. statement = statement + " -- some comment"
  8. return statement, parameters

Note

ConnectionEvents can be established on anycombination of Engine, Connection, as wellas instances of each of those classes. Events across allfour scopes will fire off for a given instance ofConnection. However, for performance reasons, theConnection object determines at instantiation timewhether or not its parent Engine has event listenersestablished. Event listeners added to the Engineclass or to an instance of Engineafter the instantiationof a dependent Connection instance will usuallynot be available on that Connection instance. The newlyadded listeners will instead take effect for Connectioninstances created subsequent to those event listeners beingestablished on the parent Engine class or instance.

  • Parameters
  • retval=False – Applies to the before_execute() andbefore_cursor_execute() events only. When True, theuser-defined event function must have a return value, whichis a tuple of parameters that replace the given statementand parameters. See those methods for a description ofspecific return arguments.

  • aftercursor_execute(_conn, cursor, statement, parameters, context, executemany)

  • Intercept low-level cursor execute() events after execution.

Example argument forms:

  1. from sqlalchemy import event
  2.  
  3. # standard decorator style
  4. @event.listens_for(SomeEngine, 'after_cursor_execute')
  5. def receive_after_cursor_execute(conn, cursor, statement, parameters, context, executemany):
  6. "listen for the 'after_cursor_execute' event"
  7.  
  8. # ... (event handling logic) ...
  9.  
  10. # named argument style (new in 0.9)
  11. @event.listens_for(SomeEngine, 'after_cursor_execute', named=True)
  12. def receive_after_cursor_execute(**kw):
  13. "listen for the 'after_cursor_execute' event"
  14. conn = kw['conn']
  15. cursor = kw['cursor']
  16.  
  17. # ... (event handling logic) ...
  1. - Parameters
  2. -
  3. -

connConnection object

  1. -

cursor – DBAPI cursor object. Will have results pendingif the statement was a SELECT, but these should not be consumedas they will be needed by the ResultProxy.

  1. -

statement – string SQL statement, as passed to the DBAPI

  1. -

parameters – Dictionary, tuple, or list of parameters beingpassed to the execute() or executemany() method of theDBAPI cursor. In some cases may be None.

  1. -

contextExecutionContext object in use. Maybe None.

  1. -

executemany – boolean, if True, this is an executemany()call, if False, this is an execute() call.

  • afterexecute(_conn, clauseelement, multiparams, params, result)
  • Intercept high level execute() events after execute.

Example argument forms:

  1. from sqlalchemy import event
  2.  
  3. # standard decorator style
  4. @event.listens_for(SomeEngine, 'after_execute')
  5. def receive_after_execute(conn, clauseelement, multiparams, params, result):
  6. "listen for the 'after_execute' event"
  7.  
  8. # ... (event handling logic) ...
  9.  
  10. # named argument style (new in 0.9)
  11. @event.listens_for(SomeEngine, 'after_execute', named=True)
  12. def receive_after_execute(**kw):
  13. "listen for the 'after_execute' event"
  14. conn = kw['conn']
  15. clauseelement = kw['clauseelement']
  16.  
  17. # ... (event handling logic) ...
  1. - Parameters
  2. -
  3. -

connConnection object

  1. -

clauseelement – SQL expression construct, Compiledinstance, or string statement passed to Connection.execute().

  1. -

multiparams – Multiple parameter sets, a list of dictionaries.

  1. -

params – Single parameter set, a single dictionary.

  1. -

resultResultProxy generated by the execution.

  • beforecursor_execute(_conn, cursor, statement, parameters, context, executemany)
  • Intercept low-level cursor execute() events before execution,receiving the string SQL statement and DBAPI-specific parameter list tobe invoked against a cursor.

Example argument forms:

  1. from sqlalchemy import event
  2.  
  3. # standard decorator style
  4. @event.listens_for(SomeEngine, 'before_cursor_execute')
  5. def receive_before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
  6. "listen for the 'before_cursor_execute' event"
  7.  
  8. # ... (event handling logic) ...
  9.  
  10. # named argument style (new in 0.9)
  11. @event.listens_for(SomeEngine, 'before_cursor_execute', named=True)
  12. def receive_before_cursor_execute(**kw):
  13. "listen for the 'before_cursor_execute' event"
  14. conn = kw['conn']
  15. cursor = kw['cursor']
  16.  
  17. # ... (event handling logic) ...

This event is a good choice for logging as well as late modificationsto the SQL string. It’s less ideal for parameter modifications exceptfor those which are specific to a target backend.

This event can be optionally established with the retval=Trueflag. The statement and parameters arguments should bereturned as a two-tuple in this case:

  1. @event.listens_for(Engine, "before_cursor_execute", retval=True)def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):

  2. # do something with statement, parameters
  3. return statement, parameters</pre>
  4. See the example at ConnectionEvents.

  5. - Parameters
  6. - 
  7.   - 
  8. conn Connection object

  9.   - 
  10. cursor DBAPI cursor object

  11.   - 
  12. statement string SQL statement, as to be passed to the DBAPI

  13.   - 
  14. parameters Dictionary, tuple, or list of parameters beingpassed to the execute() or executemany() method of theDBAPI cursor. In some cases may be None.

  15.   - 
  16. context ExecutionContext object in use. Maybe None.

  17.   - 
  18. executemany boolean, if True, this is an executemany()call, if False, this is an execute() call.

  19. See also

  20. before_execute()

  21. after_cursor_execute()

      • beforeexecute(_conn, clauseelement, multiparams, params)
      • Intercept high level execute() events, receiving uncompiledSQL constructs and other objects prior to rendering into SQL.
    • Example argument forms:

    • from sqlalchemy import event
    • # standard decorator style
    • @event.listens_for(SomeEngine, 'before_execute')
    • def receive_before_execute(conn, clauseelement, multiparams, params):
    •     "listen for the 'before_execute' event"
    •     # ... (event handling logic) ...
    • # named argument style (new in 0.9)
    • @event.listens_for(SomeEngine, 'before_execute', named=True)
    • def receive_before_execute(**kw):
    •     "listen for the 'before_execute' event"
    •     conn = kw['conn']
    •     clauseelement = kw['clauseelement']
    •     # ... (event handling logic) ...
    •  
    •  
    •  
    •  
    • This event is good for debugging SQL compilation issues as wellas early manipulation of the parameters being sent to the database,as the parameter lists will be in a consistent format here.

    • This event can be optionally established with the retval=Trueflag. The clauseelement, multiparams, and paramsarguments should be returned as a three-tuple in this case:

    • @event.listens_for(Engine, "before_execute", retval=True)def before_execute(conn, clauseelement, multiparams, params):

    • # do something with clauseelement, multiparams, params
    • return clauseelement, multiparams, params</pre>
    • - Parameters
    • - 
    •   - 
    • conn Connection object

    •   - 
    • clauseelement SQL expression construct, Compiledinstance, or string statement passed to Connection.execute().

    •   - 
    • multiparams Multiple parameter sets, a list of dictionaries.

    •   - 
    • params Single parameter set, a single dictionary.

    • See also

    • before_cursor_execute()

        • begin(conn)
        • Intercept begin() events.
      • Example argument forms:

      • from sqlalchemy import event
      • # standard decorator style
      • @event.listens_for(SomeEngine, 'begin')
      • def receive_begin(conn):
      •     "listen for the 'begin' event"
      •     # ... (event handling logic) ...
      • - Parameters
      • - 
      • conn Connection object

          • begintwophase(_conn, xid)
          • Intercept begin_twophase() events.
        • Example argument forms:

        • from sqlalchemy import event
        • # standard decorator style
        • @event.listens_for(SomeEngine, 'begin_twophase')
        • def receive_begin_twophase(conn, xid):
        •     "listen for the 'begin_twophase' event"
        •     # ... (event handling logic) ...
        • - Parameters
        • - 
        •   - 
        • conn Connection object

        •   - 
        • xid two-phase XID identifier

            • commit(conn)
            • Intercept commit() events, as initiated by aTransaction.
          • Example argument forms:

          • from sqlalchemy import event
          • # standard decorator style
          • @event.listens_for(SomeEngine, 'commit')
          • def receive_commit(conn):
          •     "listen for the 'commit' event"
          •     # ... (event handling logic) ...
          • Note that the Pool may also auto-commita DBAPI connection upon checkin, if the reset_on_returnflag is set to the value 'commit'. To intercept thiscommit, use the PoolEvents.reset() hook.

          • - Parameters
          • - 
          • conn Connection object

              • committwophase(_conn, xid, is_prepared)
              • Intercept commit_twophase() events.
            • Example argument forms:

            • from sqlalchemy import event
            • # standard decorator style
            • @event.listens_for(SomeEngine, 'commit_twophase')
            • def receive_commit_twophase(conn, xid, is_prepared):
            •     "listen for the 'commit_twophase' event"
            •     # ... (event handling logic) ...
            • - Parameters
            • - 
            •   - 
            • conn Connection object

            •   - 
            • xid two-phase XID identifier

            •   - 
            • is_prepared boolean, indicates ifTwoPhaseTransaction.prepare() was called.

                • dbapierror(_conn, cursor, statement, parameters, context, exception)
                • Intercept a raw DBAPI error.
              • Example argument forms:

              • from sqlalchemy import event
              • # standard decorator style
              • @event.listens_for(SomeEngine, 'dbapi_error')
              • def receive_dbapi_error(conn, cursor, statement, parameters, context, exception):
              •     "listen for the 'dbapi_error' event"
              •     # ... (event handling logic) ...
              • # named argument style (new in 0.9)
              • @event.listens_for(SomeEngine, 'dbapi_error', named=True)
              • def receive_dbapi_error(**kw):
              •     "listen for the 'dbapi_error' event"
              •     conn = kw['conn']
              •     cursor = kw['cursor']
              •     # ... (event handling logic) ...
              • Deprecated since version 0.9: The ConnectionEvents.dbapi_error() event is deprecated and will be removed in a future release. Please refer to the ConnectionEvents.handle_error() event.

              • This event is called with the DBAPI exception instancereceived from the DBAPI itself, before SQLAlchemy wraps theexception with its own exception wrappers, and before anyother operations are performed on the DBAPI cursor; theexisting transaction remains in effect as well as any stateon the cursor.

              • The use case here is to inject low-level exception handlinginto an Engine, typically for logging anddebugging purposes.

              • Warning

              • Code should not modifyany state or throw any exceptions here as this willinterfere with SQLAlchemys cleanup and error handlingroutines. For exception modification, please refer to thenew ConnectionEvents.handle_error() event.

              • Subsequent to this hook, SQLAlchemy may attempt anynumber of operations on the connection/cursor, includingclosing the cursor, rolling back of the transaction in thecase of connectionless execution, and disposing of the entireconnection pool if a disconnect was detected. Theexception is then wrapped in a SQLAlchemy DBAPI exceptionwrapper and re-thrown.

              • - Parameters
              • - 
              •   - 
              • conn Connection object

              •   - 
              • cursor DBAPI cursor object

              •   - 
              • statement string SQL statement, as passed to the DBAPI

              •   - 
              • parameters Dictionary, tuple, or list of parameters beingpassed to the execute() or executemany() method of theDBAPI cursor. In some cases may be None.

              •   - 
              • context ExecutionContext object in use. Maybe None.

              •   - 
              • exception The unwrapped exception emitted directly from theDBAPI. The class here is specific to the DBAPI module in use.

                  • engineconnect(_conn, branch)
                  • Intercept the creation of a new Connection.
                • Example argument forms:

                • from sqlalchemy import event
                • # standard decorator style
                • @event.listens_for(SomeEngine, 'engine_connect')
                • def receive_engine_connect(conn, branch):
                •     "listen for the 'engine_connect' event"
                •     # ... (event handling logic) ...
                • This event is called typically as the direct result of callingthe Engine.connect() method.

                • It differs from the PoolEvents.connect() method, whichrefers to the actual connection to a database at the DBAPI level;a DBAPI connection may be pooled and reused for many operations.In contrast, this event refers only to the production of a higher levelConnection wrapper around such a DBAPI connection.

                • It also differs from the PoolEvents.checkout() eventin that it is specific to the Connection object, not theDBAPI connection that PoolEvents.checkout() deals with, althoughthis DBAPI connection is available here via theConnection.connection attribute. But note there can in factbe multiple PoolEvents.checkout() events within the lifespanof a single Connection object, if that Connectionis invalidated and re-established. There can also be multipleConnection objects generated for the same already-checked-outDBAPI connection, in the case that a branch of a Connectionis produced.

                • - Parameters
                • - 
                •   - 
                • conn Connection object.

                •   - 
                • branch if True, this is a branch of an existingConnection. A branch is generated within the courseof a statement execution to invoke supplemental statements, mosttypically to pre-execute a SELECT of a default value for the purposesof an INSERT statement.

                • New in version 0.9.0.

                • See also

                • Disconnect Handling - Pessimistic - illustrates how to useConnectionEvents.engine_connect()to transparently ensure pooled connections are connected to thedatabase.

                • PoolEvents.checkout() the lower-level pool checkout eventfor an individual DBAPI connection

                • ConnectionEvents.set_connection_execution_options() - a copyof a Connection is also made when theConnection.execution_options() method is called.

                  • Example argument forms:

                  • from sqlalchemy import event
                  • # standard decorator style
                  • @event.listens_for(SomeEngine, 'engine_disposed')
                  • def receive_engine_disposed(engine):
                  •     "listen for the 'engine_disposed' event"
                  •     # ... (event handling logic) ...
                  • The Engine.dispose() method instructs the engine todispose of its connection pool (e.g. Pool), andreplaces it with a new one. Disposing of the old pool has theeffect that existing checked-in connections are closed. The newpool does not establish any new connections until it is first used.

                  • This event can be used to indicate that resources related to theEngine should also be cleaned up, keeping in mind that theEngine can still be used for new requests in which caseit re-acquires connection resources.

                  • New in version 1.0.5.

                      • handleerror(_exception_context)
                      • Intercept all exceptions processed by the Connection.
                    • Example argument forms:

                    • from sqlalchemy import event
                    • # standard decorator style
                    • @event.listens_for(SomeEngine, 'handle_error')
                    • def receive_handle_error(exception_context):
                    •     "listen for the 'handle_error' event"
                    •     # ... (event handling logic) ...
                    • This includes all exceptions emitted by the DBAPI as well aswithin SQLAlchemys statement invocation process, includingencoding errors and other statement validation errors. Other areasin which the event is invoked include transaction begin and end,result row fetching, cursor creation.

                    • Note that handle_error() may support new kinds of exceptionsand new calling scenarios at any time. Code which uses thisevent must expect new calling patterns to be present in minorreleases.

                    • To support the wide variety of members that correspond to an exception,as well as to allow extensibility of the event without backwardsincompatibility, the sole argument received is an instance ofExceptionContext. This object contains data membersrepresenting detail about the exception.

                    • Use cases supported by this hook include:

                    • - 
                    • read-only, low-level exception handling for logging anddebugging purposes

                    • - 
                    • exception re-writing

                    • - 
                    • Establishing or disabling whether a connection or the owningconnection pool is invalidated or expired in response to aspecific exception.

                    • The hook is called while the cursor from the failed operation(if any) is still open and accessible. Special cleanup operationscan be called on this cursor; SQLAlchemy will attempt to closethis cursor subsequent to this hook being invoked. If the connectionis in autocommit mode, the transaction also remains open withinthe scope of this hook; the rollback of the per-statement transactionalso occurs after the hook is called.

                    • For the common case of detecting a disconnect situation whichis not currently handled by the SQLAlchemy dialect, theExceptionContext.is_disconnect flag can be set to True whichwill cause the exception to be considered as a disconnect situation,which typically results in the connection pool being invalidated:

                    • @event.listens_for(Engine, "handle_error")def handle_exception(context):    if isinstance(context.original_exception, pyodbc.Error):        for code in (            '08S01', '01002', '08003',            '08007', '08S02', '08001', 'HYT00', 'HY010'):

                    •         if code in str(context.original_exception):
                    •             context.is_disconnect = True</pre>
                    • A handler function has two options for replacingthe SQLAlchemy-constructed exception into one that is userdefined. It can either raise this new exception directly, inwhich case all further event listeners are bypassed and theexception will be raised, after appropriate cleanup as takenplace:

                    • @event.listens_for(Engine, "handle_error")def handle_exception(context):    if isinstance(context.original_exception,        psycopg2.OperationalError) and \        "failed" in str(context.original_exception):        raise MySpecialException("failed operation")

                    • Warning

                    • Because the ConnectionEvents.handle_error()event specifically provides for exceptions to be re-thrown asthe ultimate exception raised by the failed statement,stack traces will be misleading if the user-defined eventhandler itself fails and throws an unexpected exception;the stack trace may not illustrate the actual code line thatfailed! It is advised to code carefully here and uselogging and/or inline debugging if unexpected exceptions areoccurring.

                    • Alternatively, a chained style of event handling can beused, by configuring the handler with the retval=Truemodifier and returning the new exception instance from thefunction. In this case, event handling will continue onto thenext handler. The chained exception is available usingExceptionContext.chained_exception:

                    • @event.listens_for(Engine, "handle_error", retval=True)def handle_exception(context):    if context.chained_exception is not None and \        "special" in context.chained_exception.message:        return MySpecialException("failed",            cause=context.chained_exception)

                    • Handlers that return None may be used within the chain; whena handler returns None, the previous exception instance,if any, is maintained as the current exception that is passed onto thenext handler.

                    • When a custom exception is raised or returned, SQLAlchemy raisesthis new exception as-is, it is not wrapped by any SQLAlchemyobject. If the exception is not a subclass ofsqlalchemy.exc.StatementError,certain features may not be available; currently this includesthe ORMs feature of adding a detail hint about autoflush toexceptions raised within the autoflush process.

                    • - Parameters
                    • - 
                    • context an ExceptionContext object. See thisclass for details on all available members.

                    • New in version 0.9.7: Added theConnectionEvents.handle_error() hook.

                    • Changed in version 1.1: The handle_error() event will nowreceive all exceptions that inherit from BaseException,including SystemExit and KeyboardInterrupt. The setting forExceptionContext.is_disconnect is True in this case andthe default forExceptionContext.invalidate_pool_on_disconnect isFalse.

                    • Changed in version 1.0.0: The handle_error() event is nowinvoked when an Engine fails during the initialcall to Engine.connect(), as well as when aConnection object encounters an error during areconnect operation.

                    • Changed in version 1.0.0: The handle_error() event isnot fired off when a dialect makes use of theskip_user_error_events execution option. This is usedby dialects which intend to catch SQLAlchemy-specific exceptionswithin specific operations, such as when the MySQL dialect detectsa table not present within the has_table() dialect method.Prior to 1.0.0, code which implements handle_error() needsto ensure that exceptions thrown in these scenarios are re-raisedwithout modification.

                        • preparetwophase(_conn, xid)
                        • Intercept prepare_twophase() events.
                      • Example argument forms:

                      • from sqlalchemy import event
                      • # standard decorator style
                      • @event.listens_for(SomeEngine, 'prepare_twophase')
                      • def receive_prepare_twophase(conn, xid):
                      •     "listen for the 'prepare_twophase' event"
                      •     # ... (event handling logic) ...
                      • - Parameters
                      • - 
                      •   - 
                      • conn Connection object

                      •   - 
                      • xid two-phase XID identifier

                          • releasesavepoint(_conn, name, context)
                          • Intercept release_savepoint() events.
                        • Example argument forms:

                        • from sqlalchemy import event
                        • # standard decorator style
                        • @event.listens_for(SomeEngine, 'release_savepoint')
                        • def receive_release_savepoint(conn, name, context):
                        •     "listen for the 'release_savepoint' event"
                        •     # ... (event handling logic) ...
                        • - Parameters
                        • - 
                        •   - 
                        • conn Connection object

                        •   - 
                        • name specified name used for the savepoint.

                        •   - 
                        • context ExecutionContext in use. May be None.

                            • rollback(conn)
                            • Intercept rollback() events, as initiated by aTransaction.
                          • Example argument forms:

                          • from sqlalchemy import event
                          • # standard decorator style
                          • @event.listens_for(SomeEngine, 'rollback')
                          • def receive_rollback(conn):
                          •     "listen for the 'rollback' event"
                          •     # ... (event handling logic) ...
                          • Note that the Pool also auto-rolls backa DBAPI connection upon checkin, if the reset_on_returnflag is set to its default value of 'rollback'.To intercept thisrollback, use the PoolEvents.reset() hook.

                          • - Parameters
                          • - 
                          • conn Connection object

                          • See also

                          • PoolEvents.reset()

                              • rollbacksavepoint(_conn, name, context)
                              • Intercept rollback_savepoint() events.
                            • Example argument forms:

                            • from sqlalchemy import event
                            • # standard decorator style
                            • @event.listens_for(SomeEngine, 'rollback_savepoint')
                            • def receive_rollback_savepoint(conn, name, context):
                            •     "listen for the 'rollback_savepoint' event"
                            •     # ... (event handling logic) ...
                            • - Parameters
                            • - 
                            •   - 
                            • conn Connection object

                            •   - 
                            • name specified name used for the savepoint.

                            •   - 
                            • context ExecutionContext in use. May be None.

                                • rollbacktwophase(_conn, xid, is_prepared)
                                • Intercept rollback_twophase() events.
                              • Example argument forms:

                              • from sqlalchemy import event
                              • # standard decorator style
                              • @event.listens_for(SomeEngine, 'rollback_twophase')
                              • def receive_rollback_twophase(conn, xid, is_prepared):
                              •     "listen for the 'rollback_twophase' event"
                              •     # ... (event handling logic) ...
                              • - Parameters
                              • - 
                              •   - 
                              • conn Connection object

                              •   - 
                              • xid two-phase XID identifier

                              •   - 
                              • is_prepared boolean, indicates ifTwoPhaseTransaction.prepare() was called.

                                  • savepoint(conn, name)
                                  • Intercept savepoint() events.
                                • Example argument forms:

                                • from sqlalchemy import event
                                • # standard decorator style
                                • @event.listens_for(SomeEngine, 'savepoint')
                                • def receive_savepoint(conn, name):
                                •     "listen for the 'savepoint' event"
                                •     # ... (event handling logic) ...
                                • - Parameters
                                • - 
                                •   - 
                                • conn Connection object

                                •   - 
                                • name specified name used for the savepoint.

                                  • Example argument forms:

                                  • from sqlalchemy import event
                                  • # standard decorator style
                                  • @event.listens_for(SomeEngine, 'set_connection_execution_options')
                                  • def receive_set_connection_execution_options(conn, opts):
                                  •     "listen for the 'set_connection_execution_options' event"
                                  •     # ... (event handling logic) ...
                                  • This method is called after the new Connection has beenproduced, with the newly updated execution options collection, butbefore the Dialect has acted upon any of those new options.

                                  • Note that this method is not called when a new Connectionis produced which is inheriting execution options from its parentEngine; to intercept this condition, use theConnectionEvents.engine_connect() event.

                                  • - Parameters
                                  • - 
                                  •   - 
                                  • conn The newly copied Connection object

                                  •   - 
                                  • opts dictionary of options that were passed to theConnection.execution_options() method.

                                  • New in version 0.9.0.

                                  • See also

                                  • ConnectionEvents.set_engine_execution_options() - eventwhich is called when Engine.execution_options() is called.

                                    • Example argument forms:

                                    • from sqlalchemy import event
                                    • # standard decorator style
                                    • @event.listens_for(SomeEngine, 'set_engine_execution_options')
                                    • def receive_set_engine_execution_options(engine, opts):
                                    •     "listen for the 'set_engine_execution_options' event"
                                    •     # ... (event handling logic) ...
                                    • The Engine.execution_options() method produces a shallowcopy of the Engine which stores the new options. That newEngine is passed here. A particular application of thismethod is to add a ConnectionEvents.engine_connect() eventhandler to the given Engine which will perform some per-Connection task specific to these execution options.

                                    • - Parameters
                                    • - 
                                    •   - 
                                    • conn The newly copied Engine object

                                    •   - 
                                    • opts dictionary of options that were passed to theConnection.execution_options() method.

                                    • New in version 0.9.0.

                                    • See also

                                    • ConnectionEvents.set_connection_execution_options() - eventwhich is called when Connection.execution_options() iscalled.

                                      • event interface for execution-replacement functions.

                                      • These events allow direct instrumentation and replacementof key dialect functions which interact with the DBAPI.

                                      • Note

                                      • DialectEvents hooks should be considered semi-publicand experimental.These hooks are not for general use and are only for those situationswhere intricate re-statement of DBAPI mechanics must be injected ontoan existing dialect. For general-use statement-interception events,please use the ConnectionEvents interface.

                                      • See also

                                      • ConnectionEvents.before_cursor_execute()

                                      • ConnectionEvents.before_execute()

                                      • ConnectionEvents.after_cursor_execute()

                                      • ConnectionEvents.after_execute()

                                      • New in version 0.9.4.

                                          • doconnect(_dialect, conn_rec, cargs, cparams)
                                          • Receive connection arguments before a connection is made.
                                        • Example argument forms:

                                        • from sqlalchemy import event
                                        • # standard decorator style
                                        • @event.listens_for(SomeEngine, 'do_connect')
                                        • def receive_do_connect(dialect, conn_rec, cargs, cparams):
                                        •     "listen for the 'do_connect' event"
                                        •     # ... (event handling logic) ...
                                        • # named argument style (new in 0.9)
                                        • @event.listens_for(SomeEngine, 'do_connect', named=True)
                                        • def receive_do_connect(**kw):
                                        •     "listen for the 'do_connect' event"
                                        •     dialect = kw['dialect']
                                        •     conn_rec = kw['conn_rec']
                                        •     # ... (event handling logic) ...
                                        • Return a DBAPI connection to halt further events from invoking;the returned connection will be used.

                                        • Alternatively, the event can manipulate the cargs and/or cparamscollections; cargs will always be a Python list that can be mutatedin-place and cparams a Python dictionary. Return None toallow control to pass to the next event handler and ultimatelyto allow the dialect to connect normally, given the updatedarguments.

                                        • New in version 1.0.3.

                                            • doexecute(_cursor, statement, parameters, context)
                                            • Receive a cursor to have execute() called.
                                          • Example argument forms:

                                          • from sqlalchemy import event
                                          • # standard decorator style
                                          • @event.listens_for(SomeEngine, 'do_execute')
                                          • def receive_do_execute(cursor, statement, parameters, context):
                                          •     "listen for the 'do_execute' event"
                                          •     # ... (event handling logic) ...
                                          • # named argument style (new in 0.9)
                                          • @event.listens_for(SomeEngine, 'do_execute', named=True)
                                          • def receive_do_execute(**kw):
                                          •     "listen for the 'do_execute' event"
                                          •     cursor = kw['cursor']
                                          •     statement = kw['statement']
                                          •     # ... (event handling logic) ...
                                          • Return the value True to halt further events from invoking,and to indicate that the cursor execution has already takenplace within the event handler.

                                              • doexecute_no_params(_cursor, statement, context)
                                              • Receive a cursor to have execute() with no parameters called.
                                            • Example argument forms:

                                            • from sqlalchemy import event
                                            • # standard decorator style
                                            • @event.listens_for(SomeEngine, 'do_execute_no_params')
                                            • def receive_do_execute_no_params(cursor, statement, context):
                                            •     "listen for the 'do_execute_no_params' event"
                                            •     # ... (event handling logic) ...
                                            • Return the value True to halt further events from invoking,and to indicate that the cursor execution has already takenplace within the event handler.

                                                • doexecutemany(_cursor, statement, parameters, context)
                                                • Receive a cursor to have executemany() called.
                                              • Example argument forms:

                                              • from sqlalchemy import event
                                              • # standard decorator style
                                              • @event.listens_for(SomeEngine, 'do_executemany')
                                              • def receive_do_executemany(cursor, statement, parameters, context):
                                              •     "listen for the 'do_executemany' event"
                                              •     # ... (event handling logic) ...
                                              • # named argument style (new in 0.9)
                                              • @event.listens_for(SomeEngine, 'do_executemany', named=True)
                                              • def receive_do_executemany(**kw):
                                              •     "listen for the 'do_executemany' event"
                                              •     cursor = kw['cursor']
                                              •     statement = kw['statement']
                                              •     # ... (event handling logic) ...
                                              • Return the value True to halt further events from invoking,and to indicate that the cursor execution has already takenplace within the event handler.

                                                  • dosetinputsizes(_inputsizes, cursor, statement, parameters, context)
                                                  • Receive the setinputsizes dictionary for possible modification.
                                                • Example argument forms:

                                                • from sqlalchemy import event
                                                • # standard decorator style
                                                • @event.listens_for(SomeEngine, 'do_setinputsizes')
                                                • def receive_do_setinputsizes(inputsizes, cursor, statement, parameters, context):
                                                •     "listen for the 'do_setinputsizes' event"
                                                •     # ... (event handling logic) ...
                                                • # named argument style (new in 0.9)
                                                • @event.listens_for(SomeEngine, 'do_setinputsizes', named=True)
                                                • def receive_do_setinputsizes(**kw):
                                                •     "listen for the 'do_setinputsizes' event"
                                                •     inputsizes = kw['inputsizes']
                                                •     cursor = kw['cursor']
                                                •     # ... (event handling logic) ...
                                                • This event is emitted in the case where the dialect makes use of theDBAPI cursor.setinputsizes() method which passes information aboutparameter binding for a particular statement. The giveninputsizes dictionary will contain BindParameter objectsas keys, linked to DBAPI-specific type objects as values; forparameters that are not bound, they are added to the dictionary withNone as the value, which means the parameter will not be includedin the ultimate setinputsizes call. The event may be used to inspectand/or log the datatypes that are being bound, as well as to modify thedictionary in place. Parameters can be added, modified, or removedfrom this dictionary. Callers will typically want to inspect theBindParameter.type attribute of the given bind objects inorder to make decisions about the DBAPI object.

                                                • After the event, the inputsizes dictionary is converted intoan appropriate datastructure to be passed to cursor.setinputsizes;either a list for a positional bound parameter execution style,or a dictionary of string parameter keys to DBAPI type objects fora named bound parameter execution style.

                                                • Most dialects do not use this method at all; the only built-indialect which uses this hook is the cx_Oracle dialect. The hook hereis made available so as to allow customization of how datatypes are setup with the cx_Oracle DBAPI.

                                                • New in version 1.2.9.

                                                • See also

                                                • Fine grained control over cx_Oracle data binding performance with setinputsizes

                                                • Schema Events

                                                  • Define event listeners for schema objects,that is, SchemaItem and other SchemaEventTargetsubclasses, including MetaData, Table,Column.

                                                  • MetaData and Table support eventsspecifically regarding when CREATE and DROPDDL is emitted to the database.

                                                  • Attachment events are also provided to customizebehavior whenever a child schema element is associatedwith a parent, such as, when a Column is associatedwith its Table, when a ForeignKeyConstraintis associated with a Table, etc.

                                                  • Example using the after_create event:

                                                  • from sqlalchemy import event
                                                  • from sqlalchemy import Table, Column, Metadata, Integer
                                                  • m = MetaData()
                                                  • some_table = Table('some_table', m, Column('data', Integer))
                                                  • def after_create(target, connection, **kw):
                                                  •     connection.execute("ALTER TABLE %s SET name=foo_%s" %
                                                  •                             (target.name, target.name))
                                                  • event.listen(some_table, "after_create", after_create)
                                                  • DDL events integrate closely with theDDL class and the DDLElement hierarchyof DDL clause constructs, which are themselves appropriateas listener callables:

                                                  • from sqlalchemy import DDL
                                                  • event.listen(
                                                  •     some_table,
                                                  •     "after_create",
                                                  •     DDL("ALTER TABLE %(table)s SET name=foo_%(table)s")
                                                  • )
                                                  • The methods here define the name of an event as wellas the names of members that are passed to listenerfunctions.

                                                  • For all DDLEvent events, the propagate=True keyword argumentwill ensure that a given event handler is propagated to copies of theobject, which are made when using the Table.tometadata() method:

                                                  • from sqlalchemy import DDL
                                                  • event.listen(
                                                  •     some_table,
                                                  •     "after_create",
                                                  •     DDL("ALTER TABLE %(table)s SET name=foo_%(table)s"),
                                                  •     propagate=True
                                                  • )
                                                  • new_table = some_table.tometadata(new_metadata)
                                                  • The above DDL object will also be associated with theTable object represented by new_table.

                                                  • See also

                                                  • Events

                                                  • DDLElement

                                                  • DDL

                                                  • Controlling DDL Sequences

                                                      • aftercreate(_target, connection, **kw)
                                                      • Called after CREATE statements are emitted.
                                                    • Example argument forms:

                                                    • from sqlalchemy import event
                                                    • # standard decorator style
                                                    • @event.listens_for(SomeSchemaClassOrObject, 'after_create')
                                                    • def receive_after_create(target, connection, **kw):
                                                    •     "listen for the 'after_create' event"
                                                    •     # ... (event handling logic) ...
                                                    • - Parameters
                                                    • - 
                                                    •   - 
                                                    • target the MetaData or Tableobject which is the target of the event.

                                                    •   - 
                                                    • connection the Connection where theCREATE statement or statements have been emitted.

                                                    •   - 
                                                    • **kw additional keyword arguments relevantto the event. The contents of this dictionarymay vary across releases, and include thelist of tables being generated for a metadata-levelevent, the checkfirst flag, and otherelements used by internal events.

                                                    • event.listen() also accepts the propagate=Truemodifier for this event; when True, the listener function willbe established for any copies made of the target object,i.e. those copies that are generated whenTable.tometadata() is used.

                                                        • afterdrop(_target, connection, **kw)
                                                        • Called after DROP statements are emitted.
                                                      • Example argument forms:

                                                      • from sqlalchemy import event
                                                      • # standard decorator style
                                                      • @event.listens_for(SomeSchemaClassOrObject, 'after_drop')
                                                      • def receive_after_drop(target, connection, **kw):
                                                      •     "listen for the 'after_drop' event"
                                                      •     # ... (event handling logic) ...
                                                      • - Parameters
                                                      • - 
                                                      •   - 
                                                      • target the MetaData or Tableobject which is the target of the event.

                                                      •   - 
                                                      • connection the Connection where theDROP statement or statements have been emitted.

                                                      •   - 
                                                      • **kw additional keyword arguments relevantto the event. The contents of this dictionarymay vary across releases, and include thelist of tables being generated for a metadata-levelevent, the checkfirst flag, and otherelements used by internal events.

                                                      • event.listen() also accepts the propagate=Truemodifier for this event; when True, the listener function willbe established for any copies made of the target object,i.e. those copies that are generated whenTable.tometadata() is used.

                                                        • Example argument forms:

                                                        • from sqlalchemy import event
                                                        • # standard decorator style
                                                        • @event.listens_for(SomeSchemaClassOrObject, 'after_parent_attach')
                                                        • def receive_after_parent_attach(target, parent):
                                                        •     "listen for the 'after_parent_attach' event"
                                                        •     # ... (event handling logic) ...
                                                        • - Parameters
                                                        • - 
                                                        •   - 
                                                        • target the target object

                                                        •   - 
                                                        • parent the parent to which the target is being attached.

                                                        • event.listen() also accepts the propagate=Truemodifier for this event; when True, the listener function willbe established for any copies made of the target object,i.e. those copies that are generated whenTable.tometadata() is used.

                                                            • beforecreate(_target, connection, **kw)
                                                            • Called before CREATE statements are emitted.
                                                          • Example argument forms:

                                                          • from sqlalchemy import event
                                                          • # standard decorator style
                                                          • @event.listens_for(SomeSchemaClassOrObject, 'before_create')
                                                          • def receive_before_create(target, connection, **kw):
                                                          •     "listen for the 'before_create' event"
                                                          •     # ... (event handling logic) ...
                                                          • - Parameters
                                                          • - 
                                                          •   - 
                                                          • target the MetaData or Tableobject which is the target of the event.

                                                          •   - 
                                                          • connection the Connection where theCREATE statement or statements will be emitted.

                                                          •   - 
                                                          • **kw additional keyword arguments relevantto the event. The contents of this dictionarymay vary across releases, and include thelist of tables being generated for a metadata-levelevent, the checkfirst flag, and otherelements used by internal events.

                                                          • event.listen() also accepts the propagate=Truemodifier for this event; when True, the listener function willbe established for any copies made of the target object,i.e. those copies that are generated whenTable.tometadata() is used.

                                                              • beforedrop(_target, connection, **kw)
                                                              • Called before DROP statements are emitted.
                                                            • Example argument forms:

                                                            • from sqlalchemy import event
                                                            • # standard decorator style
                                                            • @event.listens_for(SomeSchemaClassOrObject, 'before_drop')
                                                            • def receive_before_drop(target, connection, **kw):
                                                            •     "listen for the 'before_drop' event"
                                                            •     # ... (event handling logic) ...
                                                            • - Parameters
                                                            • - 
                                                            •   - 
                                                            • target the MetaData or Tableobject which is the target of the event.

                                                            •   - 
                                                            • connection the Connection where theDROP statement or statements will be emitted.

                                                            •   - 
                                                            • **kw additional keyword arguments relevantto the event. The contents of this dictionarymay vary across releases, and include thelist of tables being generated for a metadata-levelevent, the checkfirst flag, and otherelements used by internal events.

                                                            • event.listen() also accepts the propagate=Truemodifier for this event; when True, the listener function willbe established for any copies made of the target object,i.e. those copies that are generated whenTable.tometadata() is used.

                                                                • beforeparent_attach(_target, parent)
                                                                • Called before a SchemaItem is associated witha parent SchemaItem.
                                                              • Example argument forms:

                                                              • from sqlalchemy import event
                                                              • # standard decorator style
                                                              • @event.listens_for(SomeSchemaClassOrObject, 'before_parent_attach')
                                                              • def receive_before_parent_attach(target, parent):
                                                              •     "listen for the 'before_parent_attach' event"
                                                              •     # ... (event handling logic) ...
                                                              • - Parameters
                                                              • - 
                                                              •   - 
                                                              • target the target object

                                                              •   - 
                                                              • parent the parent to which the target is being attached.

                                                              • event.listen() also accepts the propagate=Truemodifier for this event; when True, the listener function willbe established for any copies made of the target object,i.e. those copies that are generated whenTable.tometadata() is used.

                                                                  • columnreflect(_inspector, table, column_info)
                                                                  • Called for each unit of column info retrieved whena Table is being reflected.
                                                                • Example argument forms:

                                                                • from sqlalchemy import event
                                                                • # standard decorator style
                                                                • @event.listens_for(SomeSchemaClassOrObject, 'column_reflect')
                                                                • def receive_column_reflect(inspector, table, column_info):
                                                                •     "listen for the 'column_reflect' event"
                                                                •     # ... (event handling logic) ...
                                                                • The dictionary of column information as returned by thedialect is passed, and can be modified. The dictionaryis that returned in each element of the list returnedby reflection.Inspector.get_columns():

                                                                  • name - the columns name

                                                                  • type - the type of this column, which should be an instanceof TypeEngine

                                                                  • nullable - boolean flag if the column is NULL or NOT NULL

                                                                  • default - the columns server default value. This isnormally specified as a plain string SQL expression, however theevent can pass a FetchedValue, DefaultClause,or sql.expression.text() object as well.

                                                                    Changed in version 1.1.6: The DDLEvents.column_reflect() event allows a nonstring FetchedValue,sql.expression.text(), or derived object to bespecified as the value of default in the columndictionary.

                                                                  • attrs - dict containing optional column attributes

                                                                • The event is called before any action is taken againstthis dictionary, and the contents can be modified.The Column specific arguments info, key,and quote can also be added to the dictionary andwill be passed to the constructor of Column.

                                                                • Note that this event is only meaningful if eitherassociated with the Table class across theboard, e.g.:

                                                                • from sqlalchemy.schema import Table
                                                                • from sqlalchemy import event
                                                                • def listen_for_reflect(inspector, table, column_info):
                                                                •     "receive a column_reflect event"
                                                                •     # ...
                                                                • event.listen(
                                                                •         Table,
                                                                •         'column_reflect',
                                                                •         listen_for_reflect)
                                                                • or with a specific Table instance usingthe listeners argument:

                                                                • def listen_for_reflect(inspector, table, column_info):
                                                                •     "receive a column_reflect event"
                                                                •     # ...
                                                                • t = Table(
                                                                •     'sometable',
                                                                •     autoload=True,
                                                                •     listeners=[
                                                                •         ('column_reflect', listen_for_reflect)
                                                                •     ])
                                                                • This because the reflection process initiated by autoload=Truecompletes within the scope of the constructor for Table.

                                                                • event.listen() also accepts the propagate=Truemodifier for this event; when True, the listener function willbe established for any copies made of the target object,i.e. those copies that are generated whenTable.tometadata() is used.

                                                                    • class sqlalchemy.events.SchemaEventTarget
                                                                    • Base class for elements that are the targets of DDLEventsevents.
                                                                  • This includes SchemaItem as well as SchemaType.