summaryrefslogtreecommitdiff
path: root/test/sql
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2015-05-08 12:37:55 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2015-05-08 12:37:55 -0400
commiteb1bb84fbc10c801c7269a3d38c9e0235327857e (patch)
treed498ddd719456996a8169d48d18271acb291a069 /test/sql
parent5c897d9c6a84ac0cc1d1300dc2456a14b6e6bdd8 (diff)
downloadsqlalchemy-eb1bb84fbc10c801c7269a3d38c9e0235327857e.tar.gz
- Added official support for a CTE used by the SELECT present
inside of :meth:`.Insert.from_select`. This behavior worked accidentally up until 0.9.9, when it no longer worked due to unrelated changes as part of :ticket:`3248`. Note that this is the rendering of the WITH clause after the INSERT, before the SELECT; the full functionality of CTEs rendered at the top level of INSERT, UPDATE, DELETE is a new feature targeted for a later release. fixes #3418
Diffstat (limited to 'test/sql')
-rw-r--r--test/sql/test_insert.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/test/sql/test_insert.py b/test/sql/test_insert.py
index 7170fcbcb..3c533d75f 100644
--- a/test/sql/test_insert.py
+++ b/test/sql/test_insert.py
@@ -176,6 +176,41 @@ class InsertTest(_InsertTestBase, fixtures.TablesTest, AssertsCompiledSQL):
checkparams={"name_1": "foo"}
)
+ def test_insert_from_select_cte_one(self):
+ table1 = self.tables.mytable
+
+ cte = select([table1.c.name]).where(table1.c.name == 'bar').cte()
+
+ sel = select([table1.c.myid, table1.c.name]).where(
+ table1.c.name == cte.c.name)
+
+ ins = self.tables.myothertable.insert().\
+ from_select(("otherid", "othername"), sel)
+ self.assert_compile(
+ ins,
+ "INSERT INTO myothertable (otherid, othername) WITH anon_1 AS "
+ "(SELECT mytable.name AS name FROM mytable "
+ "WHERE mytable.name = :name_1) "
+ "SELECT mytable.myid, mytable.name FROM mytable, anon_1 "
+ "WHERE mytable.name = anon_1.name",
+ checkparams={"name_1": "bar"}
+ )
+
+ def test_insert_from_select_cte_two(self):
+ table1 = self.tables.mytable
+
+ cte = table1.select().cte("c")
+ stmt = cte.select()
+ ins = table1.insert().from_select(table1.c, stmt)
+
+ self.assert_compile(
+ ins,
+ "INSERT INTO mytable (myid, name, description) "
+ "WITH c AS (SELECT mytable.myid AS myid, mytable.name AS name, "
+ "mytable.description AS description FROM mytable) "
+ "SELECT c.myid, c.name, c.description FROM c"
+ )
+
def test_insert_from_select_select_alt_ordering(self):
table1 = self.tables.mytable
sel = select([table1.c.name, table1.c.myid]).where(