]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
reset key/name when TableValuedColumn is adapted
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 16 Jul 2021 14:14:56 +0000 (10:14 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 16 Jul 2021 14:14:56 +0000 (10:14 -0400)
Fixed issue in new :meth:`_schema.Table.table_valued` method where the
resulting :class:`_sql.TableValuedColumn` construct would not respond
correctly to alias adaptation as is used throughout the ORM, such as for
eager loading, polymorphic loading, etc.

Fixes: #6775
Change-Id: I77cec4b6e1b1003f2b6be242b54ada8e4a435250

doc/build/changelog/unreleased_14/6775.rst [new file with mode: 0644]
lib/sqlalchemy/sql/elements.py
test/sql/test_external_traversal.py

diff --git a/doc/build/changelog/unreleased_14/6775.rst b/doc/build/changelog/unreleased_14/6775.rst
new file mode 100644 (file)
index 0000000..e7c9a53
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, orm
+    :tickets: 6775
+
+    Fixed issue in new :meth:`_schema.Table.table_valued` method where the
+    resulting :class:`_sql.TableValuedColumn` construct would not respond
+    correctly to alias adaptation as is used throughout the ORM, such as for
+    eager loading, polymorphic loading, etc.
+
index f95fa143e9aba83a6f110d778b0e875eee833bd5..e253ddb936f312b28e79060262b8df714bcf81ed 100644 (file)
@@ -4931,6 +4931,10 @@ class TableValuedColumn(NamedColumn):
         self.key = self.name = scalar_alias.name
         self.type = type_
 
+    def _copy_internals(self, clone=_clone, **kw):
+        self.scalar_alias = clone(self.scalar_alias, **kw)
+        self.key = self.name = self.scalar_alias.name
+
     @property
     def _from_objects(self):
         return [self.scalar_alias]
index b0241da5901f6387fbb05d913026b2e56616a215..764bcd6d4acd315540ebd17cef036663f23474e0 100644 (file)
@@ -2231,6 +2231,21 @@ class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL):
             "FROM table1 AS table1_1",
         )
 
+    def test_table_valued_column(self):
+        """test #6775"""
+        stmt = select(func.some_json_func(t1.table_valued()))
+
+        self.assert_compile(
+            stmt,
+            "SELECT some_json_func(table1) AS some_json_func_1 FROM table1",
+        )
+
+        self.assert_compile(
+            sql_util.ClauseAdapter(t1.alias()).traverse(stmt),
+            "SELECT some_json_func(table1_1) AS some_json_func_1 "
+            "FROM table1 AS table1_1",
+        )
+
     def test_recursive(self):
         metadata = MetaData()
         a = Table("a", metadata, Column("id", Integer, primary_key=True))