]> 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:02 +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

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 2e8f41cc8369a7183dcbc95e405aa60d2e212630..5fc83815d0cc972805461dae4dd280927b268fb7 100644 (file)
@@ -2168,6 +2168,13 @@ class TableClause(roles.DMLTableRole, 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 '%s'"
+                % (c.key, existing)
+            )
+
         self._columns.add(c)
         c.table = self
 
index ebcde3c6310a99adca3a5f1f1b0da5e4aea6ce08..d378c8184daa6225bbae41acf003bb4eaf6f54d8 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
@@ -3694,7 +3696,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)
 
@@ -3707,6 +3709,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