found_table = False
for col_d in self.get_columns(table_name, schema, **tblkw):
found_table = True
+ table.dispatch.column_reflect(table, col_d)
+
name = col_d['name']
if include_columns and name not in include_columns:
continue
col_kw = {
'nullable':col_d['nullable'],
}
- if 'autoincrement' in col_d:
- col_kw['autoincrement'] = col_d['autoincrement']
- if 'quote' in col_d:
- col_kw['quote'] = col_d['quote']
+ for k in ('autoincrement', 'quote', 'info', 'key'):
+ if k in col_d:
+ col_kw[k] = col_d[k]
colargs = []
if col_d.get('default') is not None:
"""
+ def column_reflect(self, table, column_info):
+ """Called for each unit of 'column info' retrieved when
+ a :class:`.Table` is being reflected.
+
+ The dictionary of column information as returned by the
+ dialect is passed, and can be modified. The dictionary
+ is that returned in each element of the list returned
+ by :meth:`.reflection.Inspector.get_columns`.
+
+ The event is called before any action is taken against
+ this dictionary, and the contents can be modified.
+ The :class:`.Column` specific arguments `info`, `key`,
+ and `quote` can also be added to the dictionary and
+ will be passed to the constructor of :class:`.Column`.
+
+ Note that this event is only meaningful if either
+ associated with the :class:`.Table` class across the
+ board, e.g.::
+
+ from sqlalchemy.schema import Table
+ from sqlalchemy import event
+
+ def listen_for_reflect(table, column_info):
+ "receive a column_reflect event"
+ # ...
+
+ event.listen(
+ Table,
+ 'column_reflect',
+ listen_for_reflect)
+
+ ...or with a specific :class:`.Table` instance using
+ the ``listeners`` argument::
+
+ def listen_for_reflect(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=True``
+ completes within the scope of the constructor for :class:`.Table`.
+
+ """
+
class SchemaEventTarget(object):
"""Base class for elements that are the targets of :class:`.DDLEvents` events.
:param info: A dictionary which defaults to ``{}``. A space to store
application specific data. This must be a dictionary.
+ :param listeners: A list of tuples of the form ``(<eventname>, <fn>)``
+ which will be passed to :func:`.event.listen` upon construction.
+ This alternate hook to :func:`.event.listen` allows the establishment
+ of a listener function specific to this :class:`.Table` before
+ the "autoload" process begins. Particularly useful for
+ the :meth:`.events.column_reflect` event::
+
+ def listen_for_reflect(table, column_info):
+ # ...
+
+ t = Table(
+ 'sometable',
+ autoload=True,
+ listeners=[
+ ('column_reflect', listen_for_reflect)
+ ])
+
:param mustexist: When ``True``, indicates that this Table must already
be present in the given :class:`MetaData`` collection.
self.quote_schema = kwargs.pop('quote_schema', None)
if 'info' in kwargs:
self.info = kwargs.pop('info')
+ if 'listeners' in kwargs:
+ listeners = kwargs.pop('listeners')
+ for evt, fn in listeners:
+ event.listen(self, evt, fn)
self._prefixes = kwargs.pop('prefixes', [])