diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-09-30 08:37:57 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-09-30 09:37:02 -0400 |
commit | 9586594754cef19bf1e2e14b7e70876efefdafbe (patch) | |
tree | c9a02a1ab191d0cb8ac8f0281f2d7e223f40fb5d | |
parent | b21a03316ff35ea86405f07d70fa1a2de7a01378 (diff) | |
download | sqlalchemy-9586594754cef19bf1e2e14b7e70876efefdafbe.tar.gz |
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
-rw-r--r-- | doc/build/changelog/unreleased_13/5618.rst | 8 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/selectable.py | 7 | ||||
-rw-r--r-- | test/sql/test_metadata.py | 16 |
3 files changed, 30 insertions, 1 deletions
diff --git a/doc/build/changelog/unreleased_13/5618.rst b/doc/build/changelog/unreleased_13/5618.rst new file mode 100644 index 000000000..ab4d16359 --- /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 2e8f41cc8..5fc83815d 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 ebcde3c63..d378c8184 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 |