summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Caselli <cfederico87@gmail.com>2023-02-01 22:25:03 +0100
committerMike Bayer <mike_mp@zzzcomputing.com>2023-02-05 11:35:32 -0500
commita09972dcab2d154044cfd2d60bf72e4a3f7568f1 (patch)
tree45e340dfb6ed106e8e179af35994ce0b0dea4683
parent0635235090c85e2c1a18676ca49652d2c2094925 (diff)
downloadsqlalchemy-a09972dcab2d154044cfd2d60bf72e4a3f7568f1.tar.gz
Guard against wrong dataclass mapping
Ensure that the decorator style @registry.mapped_as_dataclass and MappedAsDataclass are not mixed. Fixes: #9211 Change-Id: I5cd94cae862122e4f627d0d051495b3789cf6de5
-rw-r--r--doc/build/changelog/unreleased_20/9211.rst10
-rw-r--r--lib/sqlalchemy/orm/decl_base.py11
-rw-r--r--test/orm/declarative/test_dc_transforms.py17
3 files changed, 38 insertions, 0 deletions
diff --git a/doc/build/changelog/unreleased_20/9211.rst b/doc/build/changelog/unreleased_20/9211.rst
new file mode 100644
index 000000000..f15a64b05
--- /dev/null
+++ b/doc/build/changelog/unreleased_20/9211.rst
@@ -0,0 +1,10 @@
+.. change::
+ :tags: bug, orm
+ :tickets: 9211
+
+ An explicit error is raised if a mapping attempts to mix the use of
+ :class:`_orm.MappedAsDataclass` with
+ :meth:`_orm.registry.mapped_as_dataclass` within the same class hierarchy,
+ as this produces issues with the dataclass function being applied at the
+ wrong time to the mapped class, leading to errors during the mapping
+ process.
diff --git a/lib/sqlalchemy/orm/decl_base.py b/lib/sqlalchemy/orm/decl_base.py
index a858f12cb..8ce9d59d0 100644
--- a/lib/sqlalchemy/orm/decl_base.py
+++ b/lib/sqlalchemy/orm/decl_base.py
@@ -1036,6 +1036,17 @@ class _ClassScanMapperConfig(_MapperConfig):
if not dataclass_setup_arguments:
return
+ # can't use is_dataclass since it uses hasattr
+ if "__dataclass_fields__" in self.cls.__dict__:
+ raise exc.InvalidRequestError(
+ f"Class {self.cls} is already a dataclass; ensure that "
+ "base classes / decorator styles of establishing dataclasses "
+ "are not being mixed. "
+ "This can happen if a class that inherits from "
+ "'MappedAsDataclass', even indirectly, is been mapped with "
+ "'@registry.mapped_as_dataclass'"
+ )
+
manager = instrumentation.manager_of_class(self.cls)
assert manager is not None
diff --git a/test/orm/declarative/test_dc_transforms.py b/test/orm/declarative/test_dc_transforms.py
index 63450f4a1..302587b9a 100644
--- a/test/orm/declarative/test_dc_transforms.py
+++ b/test/orm/declarative/test_dc_transforms.py
@@ -629,6 +629,23 @@ class DCTransformsTest(AssertsCompiledSQL, fixtures.TestBase):
b1 = Bar(mixin_value=5)
eq_(b1.bar_value, 78)
+ def test_mixing_MappedAsDataclass_with_decorator_raises(self, registry):
+ """test #9211"""
+
+ class Mixin(MappedAsDataclass):
+ id: Mapped[int] = mapped_column(primary_key=True, init=False)
+
+ with expect_raises_message(
+ exc.InvalidRequestError,
+ "Class .*Foo.* is already a dataclass; ensure that "
+ "base classes / decorator styles of establishing dataclasses "
+ "are not being mixed. ",
+ ):
+
+ @registry.mapped_as_dataclass
+ class Foo(Mixin):
+ bar_value: Mapped[float] = mapped_column(default=78)
+
class RelationshipDefaultFactoryTest(fixtures.TestBase):
def test_list(self, dc_decl_base: Type[MappedAsDataclass]):