:param default: A scalar, Python callable, or
~sqlalchemy.sql.expression.ClauseElement` representing the
+ :class:` representing the
*default value* for this column, which will be invoked upon insert
if this column is otherwise not specified in the VALUES clause of
the insert. This is a shortcut to using :class:`.ColumnDefault` as
+ a positional argument; see that class for full detail on the
+ structure of the argument.
Contrast this argument to ``server_default`` which creates a
default generator on the database side.
def __init__(self, arg, **kwargs):
+ """"Construct a new :class:`.ColumnDefault`.
+ :param arg: argument representing the default value.
+ May be one of the following:
+ * a plain non-callable Python value, such as a
+ string, integer, boolean, or other simple type.
+ The default value will be used as is each time.
+ * a SQL expression, that is one which derives from
+ :class:`.ColumnElement`. The SQL expression will
+ be rendered into the INSERT or UPDATE statement,
+ or in the case of a primary key column when
+ RETURNING is not used may be
+ pre-executed before an INSERT within a SELECT.
+ * A Python callable. The function will be invoked for each
+ new row subject to an INSERT or UPDATE.
+ The callable must accept exactly
+ zero or one positional arguments. The one-argument form
+ will receive an instance of the :class:`.ExecutionContext`,
+ which provides contextual information as to the current
+ :class:`.Connection` in use as well as the current
+ statement and parameters.
if isinstance(arg, FetchedValue):
def _maybe_wrap_callable(self, fn):
- """Backward compat: Wrap callables that don't accept a context."""
+ """Wrap callables that don't accept a context.
+ The alternative here is to require that
+ a simple callable passed to "default" would need
+ to be of the form "default=lambda ctx: datetime.now".
+ That is the more "correct" way to go, but the case
+ of using a zero-arg callable for "default" is so
+ much more prominent than the context-specific one
+ I'm having trouble justifying putting that inconvenience
- positionals = len(argspec)
+ defaulted = argspec is not None and len(argspec) or 0
+ positionals = len(argspec) - defaulted
# Py3K compat - no unbound methods
if inspect.ismethod(inspectable) or inspect.isclass(fn):
- defaulted = argspec is not None and len(argspec) or 0
- if positionals - defaulted > 1:
"ColumnDefault Python function takes zero or one "