]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
allow column named twice warning to take effect
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 9 May 2023 15:33:30 +0000 (11:33 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 9 May 2023 15:34:17 +0000 (11:34 -0400)
Fixed issue in :func:`_orm.mapped_column` construct where the correct
warning for "column X named directly multiple times" would not be emitted
when ORM mapped attributes referred to the same :class:`_schema.Column`, if
the :func:`_orm.mapped_column` construct were involved, raising an internal
assertion instead.

Fixes: #9630
Change-Id: I5d9dfaaa225aefb487c9cd981ba3ad78507bb577

doc/build/changelog/unreleased_20/9630.rst [new file with mode: 0644]
lib/sqlalchemy/orm/decl_base.py
test/orm/declarative/test_basic.py

diff --git a/doc/build/changelog/unreleased_20/9630.rst b/doc/build/changelog/unreleased_20/9630.rst
new file mode 100644 (file)
index 0000000..912cb57
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, orm
+    :tickets: 9630
+
+    Fixed issue in :func:`_orm.mapped_column` construct where the correct
+    warning for "column X named directly multiple times" would not be emitted
+    when ORM mapped attributes referred to the same :class:`_schema.Column`, if
+    the :func:`_orm.mapped_column` construct were involved, raising an internal
+    assertion instead.
index b7d6dd8cfbf3c433f8c141f32ac31178165b9666..71b93de8f71d471ad46d7e5f54924651fbd6f302 100644 (file)
@@ -1646,7 +1646,11 @@ class _ClassScanMapperConfig(_MapperConfig):
                     if not isinstance(c, CompositeProperty):
                         name_to_prop_key[col.name].add(key)
                     declared_columns.add(col)
-                    assert col not in column_ordering
+
+                    # we would assert this, however we want the below
+                    # warning to take effect instead.  See #9630
+                    # assert col not in column_ordering
+
                     column_ordering[col] = sort_order
 
                     # if this is a MappedColumn and the attribute key we
index d0e56819cbb91e18f8dec9cdbb48a7f8cfb5b328..2ccbc19f1a025aca21946e5db7ec90bbcdd0295c 100644 (file)
@@ -1419,20 +1419,35 @@ class DeclarativeMultiBaseTest(
                 x = Column("x", Integer)
                 y = Column("x", Integer)
 
-    def test_column_repeated_under_prop(self):
+    @testing.variation("style", ["old", "new"])
+    def test_column_repeated_under_prop(self, style):
         with expect_warnings(
             "On class 'Foo', Column object 'x' named directly multiple "
             "times, only one will be used: x, y, z. Consider using "
             "orm.synonym instead"
         ), expect_raises(exc.DuplicateColumnError):
+            if style.old:
 
-            class Foo(Base):
-                __tablename__ = "foo"
+                class Foo(Base):
+                    __tablename__ = "foo"
 
-                id = Column(Integer, primary_key=True)
-                x = Column("x", Integer)
-                y = column_property(x)
-                z = Column("x", Integer)
+                    id = Column(Integer, primary_key=True)
+                    x = Column("x", Integer)
+                    y = column_property(x)
+                    z = Column("x", Integer)
+
+            elif style.new:
+
+                class Foo(Base):
+                    __tablename__ = "foo"
+
+                    id = mapped_column(Integer, primary_key=True)
+                    x = mapped_column("x", Integer)
+                    y = column_property(x)
+                    z = mapped_column("x", Integer)
+
+            else:
+                style.fail()
 
     def test_using_explicit_prop_in_schema_objects(self):
         class Foo(Base):