diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2017-02-27 16:43:59 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2017-03-30 14:58:50 -0400 |
| commit | 4eb4010c1a1c3e5c2529b9be9d8d56f1d6a4ec00 (patch) | |
| tree | bac9c2f192b43d097ee9122a1add4c4f952d2d8e /lib/sqlalchemy | |
| parent | 74f6c21747d07a8cf9902900df9280a84aadc2bb (diff) | |
| download | sqlalchemy-4eb4010c1a1c3e5c2529b9be9d8d56f1d6a4ec00.tar.gz | |
Apply type processing to untyped preexec default clause
Fixed bug where a SQL-oriented Python-side column default could fail to
be executed properly upon INSERT in the "pre-execute" codepath, if the
SQL itself were an untyped expression, such as plain text. The "pre-
execute" codepath is fairly uncommon however can apply to non-integer
primary key columns with SQL defaults when RETURNING is not used.
Tests exist here to ensure typing is applied to
a typed expression for default, but in the case of
an untyped SQL value, we know the type from the column,
so apply this.
Change-Id: I5d8b391611c137b9f700115a50a2bf5b30abfe94
Fixes: #3923
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/engine/default.py | 6 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/schema.py | 8 |
2 files changed, 13 insertions, 1 deletions
diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index c7d574a21..1c10f484f 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -1073,7 +1073,11 @@ class DefaultExecutionContext(interfaces.ExecutionContext): # TODO: expensive branching here should be # pulled into _exec_scalar() conn = self.connection - c = expression.select([default.arg]).compile(bind=conn) + if not default._arg_is_typed: + default_arg = expression.type_coerce(default.arg, type_) + else: + default_arg = default.arg + c = expression.select([default_arg]).compile(bind=conn) return conn._execute_compiled(c, (), {}).scalar() else: return default.arg diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index f8d3209ef..cf12ce965 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -2067,6 +2067,14 @@ class ColumnDefault(DefaultGenerator): not self.is_clause_element and \ not self.is_sequence + @util.memoized_property + @util.dependencies("sqlalchemy.sql.sqltypes") + def _arg_is_typed(self, sqltypes): + if self.is_clause_element: + return not isinstance(self.arg.type, sqltypes.NullType) + else: + return False + def _maybe_wrap_callable(self, fn): """Wrap callables that don't accept a context. |
