]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
raise on lower-case column shared to multiple tables
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 30 Sep 2020 12:37:57 +0000 (08:37 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 30 Sep 2020 13:37:36 +0000 (09:37 -0400)
Fixed bug where an error was not raised for lower-case
:func:`_column` added to lower-case :func:`_table` object. This now raises
:class:`_exc.ArgumentError` which has always been the case for
upper-case :class:`_schema.Column` and :class:`_schema.Table`.

Fixes: #5618
Change-Id: Ifcbdf27c022fd2996a5b99559df71fc1c1e0f19c
(cherry picked from commit ba0febcf54f194fc93d40a6bf72502ee50ba9993)

doc/build/changelog/unreleased_13/5618.rst [new file with mode: 0644]
lib/sqlalchemy/sql/selectable.py
test/sql/test_metadata.py

diff --git a/doc/build/changelog/unreleased_13/5618.rst b/doc/build/changelog/unreleased_13/5618.rst
new file mode 100644 (file)
index 0000000..ab4d163
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, sql
+    :tickets: 5618
+
+    Fixed bug where an error was not raised for lower-case :func:`_sql.column`
+    added to lower-case :func:`_sql.table` object. This now raises
+    :class:`_exc.ArgumentError` which has always been the case for upper-case
+    :class:`_schema.Column` and :class:`_schema.Table`.
\ No newline at end of file
index 5a13444edbeb5d1fb611071ea9e2e8e828ce33dc..370258592103d153fcb7d0a86fb12a9e4cead123 100644 (file)
@@ -1979,6 +1979,13 @@ class TableClause(Immutable, FromClause):
             return self.name.encode("ascii", "backslashreplace")
 
     def append_column(self, c):
+        existing = c.table
+        if existing is not None and existing is not self:
+            raise exc.ArgumentError(
+                "column object '%s' already assigned to table %r"
+                % (c.key, getattr(existing, "description", existing))
+            )
+
         self._columns[c.key] = c
         c.table = self
 
index 5e1fdbddf25ee10a2725c6230e7ab1dc4fed9a8e..fee1fbb7497cc7e53bbeec9cfce1f340dcc0b17b 100644 (file)
@@ -8,6 +8,7 @@ from sqlalchemy import BLANK_SCHEMA
 from sqlalchemy import Boolean
 from sqlalchemy import CheckConstraint
 from sqlalchemy import Column
+from sqlalchemy import column
 from sqlalchemy import ColumnDefault
 from sqlalchemy import desc
 from sqlalchemy import Enum
@@ -24,6 +25,7 @@ from sqlalchemy import schema
 from sqlalchemy import Sequence
 from sqlalchemy import String
 from sqlalchemy import Table
+from sqlalchemy import table
 from sqlalchemy import testing
 from sqlalchemy import text
 from sqlalchemy import TypeDecorator
@@ -3606,7 +3608,7 @@ class ColumnDefinitionTest(AssertsCompiledSQL, fixtures.TestBase):
             c,
         )
 
-    def test_dupe_column(self):
+    def test_no_shared_column_schema(self):
         c = Column("x", Integer)
         Table("t", MetaData(), c)
 
@@ -3619,6 +3621,18 @@ class ColumnDefinitionTest(AssertsCompiledSQL, fixtures.TestBase):
             c,
         )
 
+    def test_no_shared_column_sql(self):
+        c = column("x", Integer)
+        table("t", c)
+
+        assert_raises_message(
+            exc.ArgumentError,
+            "column object 'x' already assigned to table 't'",
+            table,
+            "q",
+            c,
+        )
+
     def test_incomplete_key(self):
         c = Column(Integer)
         assert c.name is None