diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-09-16 13:38:11 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-09-16 13:40:47 -0400 |
| commit | 8fca5b6e6a42b6221faaf26a912603393afd8607 (patch) | |
| tree | d06793fc78d9835b040bf2a9d14b35e3da46162a /lib/sqlalchemy/sql | |
| parent | c27bf6f7ecc1a120f0c8029eaa5969b6d2aa255d (diff) | |
| download | sqlalchemy-8fca5b6e6a42b6221faaf26a912603393afd8607.tar.gz | |
use the stack for insert_from_select
Fixed issue related to new ``add_cte()`` feature where pairing two
"INSERT..FROM SELECT" statements simultaneously would lose track of the two
independent SELECT statements, leading to the wrong SQL.
Fixes: #7036
Change-Id: I90fe47eb203bc5c1ea5810db0edba08250b2b7e6
Diffstat (limited to 'lib/sqlalchemy/sql')
| -rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 5 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/crud.py | 12 |
2 files changed, 12 insertions, 5 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 9af82823a..5153f54d1 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -3687,7 +3687,10 @@ class SQLCompiler(Compiled): returning_clause = None if insert_stmt.select is not None: - select_text = self.process(self._insert_from_select, **kw) + # placed here by crud.py + select_text = self.process( + self.stack[-1]["insert_from_select"], **kw + ) if self.ctes and toplevel and self.dialect.cte_follows_insert: text += " %s%s" % (self._render_cte_clause(), select_text) diff --git a/lib/sqlalchemy/sql/crud.py b/lib/sqlalchemy/sql/crud.py index b8f8cb4ce..d43f33ebb 100644 --- a/lib/sqlalchemy/sql/crud.py +++ b/lib/sqlalchemy/sql/crud.py @@ -310,7 +310,9 @@ def _scan_insert_from_select_cols( cols = [stmt.table.c[_column_as_key(name)] for name in stmt._select_names] - compiler._insert_from_select = stmt.select + assert compiler.stack[-1]["selectable"] is stmt + + compiler.stack[-1]["insert_from_select"] = stmt.select add_select_cols = [] if stmt.include_insert_from_select_defaults: @@ -331,10 +333,12 @@ def _scan_insert_from_select_cols( if add_select_cols: values.extend(add_select_cols) - compiler._insert_from_select = compiler._insert_from_select._generate() - compiler._insert_from_select._raw_columns = tuple( - compiler._insert_from_select._raw_columns + ins_from_select = compiler.stack[-1]["insert_from_select"] + ins_from_select = ins_from_select._generate() + ins_from_select._raw_columns = tuple( + ins_from_select._raw_columns ) + tuple(expr for col, col_expr, expr in add_select_cols) + compiler.stack[-1]["insert_from_select"] = ins_from_select def _scan_cols( |
