summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/compiler.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-12-19 16:02:14 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2013-12-19 16:02:14 -0500
commit5f76f29c15b7a23cfe29c5fbd22ad02452b6a2c0 (patch)
tree022779ebba8df3b7d75d4c46fa7730f0f2d1fda5 /lib/sqlalchemy/sql/compiler.py
parent47eb5682d1b8885c052e4bc50004af45b5f19174 (diff)
downloadsqlalchemy-5f76f29c15b7a23cfe29c5fbd22ad02452b6a2c0.tar.gz
- Fixed bug with :meth:`.Insert.from_select` method where the order
of the given names would not be taken into account when generating the INSERT statement, thus producing a mismatch versus the column names in the given SELECT statement. Also noted that :meth:`.Insert.from_select` implies that Python-side insert defaults cannot be used, since the statement has no VALUES clause. [ticket:2895]
Diffstat (limited to 'lib/sqlalchemy/sql/compiler.py')
-rw-r--r--lib/sqlalchemy/sql/compiler.py15
1 files changed, 11 insertions, 4 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index 1d38c9ad3..bd886bd40 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -1965,10 +1965,17 @@ class SQLCompiler(Compiled):
elif c.server_onupdate is not None:
self.postfetch.append(c)
- # iterating through columns at the top to maintain ordering.
- # otherwise we might iterate through individual sets of
- # "defaults", "primary key cols", etc.
- for c in stmt.table.columns:
+ if self.isinsert and stmt.select_names:
+ # for an insert from select, we can only use names that
+ # are given, so only select for those names.
+ cols = (stmt.table.c[elements._column_as_key(name)]
+ for name in stmt.select_names)
+ else:
+ # iterate through all table columns to maintain
+ # ordering, even for those cols that aren't included
+ cols = stmt.table.columns
+
+ for c in cols:
if c.key in parameters and c.key not in check_columns:
value = parameters.pop(c.key)
if elements._is_literal(value):