From: Mike Bayer Date: Wed, 30 Sep 2020 12:37:57 +0000 (-0400) Subject: raise on lower-case column shared to multiple tables X-Git-Tag: rel_1_4_0b1~62^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9586594754cef19bf1e2e14b7e70876efefdafbe;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git raise on lower-case column shared to multiple tables 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 --- diff --git a/doc/build/changelog/unreleased_13/5618.rst b/doc/build/changelog/unreleased_13/5618.rst new file mode 100644 index 0000000000..ab4d16359d --- /dev/null +++ b/doc/build/changelog/unreleased_13/5618.rst @@ -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 diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index 2e8f41cc83..5fc83815d0 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -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 diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index ebcde3c631..d378c8184d 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -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