INSERT from SELECT UNION

Issue #3044 resolved
Mike Bayer repo owner created an issue

see if we don't need the subquery here:

from sqlalchemy import *
from sqlalchemy.sql import table, column

product = table('product', column('id'), column('other_id'))

b_id = 2
s_id = 3
id = product.c.id
sel = select(
    [b_id, product.c.id],
).union(
    select([b_id, s_id])
)

ins = insert(product).from_select([
    product.c.id, product.c.other_id
    ],
    sel
)

print ins

patch:

index 61abe81..2b4320a 100644
--- a/lib/sqlalchemy/sql/selectable.py
+++ b/lib/sqlalchemy/sql/selectable.py
@@ -42,7 +42,7 @@ def _interpret_as_select(element):
     element = _interpret_as_from(element)
     if isinstance(element, Alias):
         element = element.original
-    if not isinstance(element, Select):
+    if not isinstance(element, SelectBase):
         element = element.select()
     return element

Comments (3)

  1. Mike Bayer reporter
    • Fixed bug in INSERT..FROM SELECT construct where selecting from a UNION would wrap the union in an anonymous (e.g. unlabled) subquery. fixes #3044

    → <<cset 076eef5afb47>>

  2. Mike Bayer reporter
    • Fixed bug in INSERT..FROM SELECT construct where selecting from a UNION would wrap the union in an anonymous (e.g. unlabled) subquery. fixes #3044

    → <<cset 83b2e9e8d250>>

  3. Log in to comment