--- /dev/null
+.. change::
+ :tags: orm, bug
+ :tickets: 11973
+
+ Improved the error message emitted when trying to map as dataclass a class
+ while also manually providing the ``__table__`` attribute.
+ This usage is currently not supported.
"'@registry.mapped_as_dataclass'"
)
+ # can't create a dataclass if __table__ is already there. This would
+ # fail an assertion when calling _get_arguments_for_make_dataclass:
+ # assert False, "Mapped[] received without a mapping declaration"
+ if "__table__" in self.cls.__dict__:
+ raise exc.InvalidRequestError(
+ f"Class {self.cls} already defines a '__table__'. "
+ "ORM Annotated Dataclasses do not support a pre-existing "
+ "'__table__' element"
+ )
+
warn_for_non_dc_attrs = collections.defaultdict(list)
def _allow_dataclass_field(
from sqlalchemy import JSON
from sqlalchemy import select
from sqlalchemy import String
+from sqlalchemy import Table
from sqlalchemy import testing
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import column_property
class Foo(Mixin):
bar_value: Mapped[float] = mapped_column(default=78)
+ def test_MappedAsDataclass_table_provided(self, registry):
+ """test #11973"""
+
+ with expect_raises_message(
+ exc.InvalidRequestError,
+ "Class .*Foo.* already defines a '__table__'. "
+ "ORM Annotated Dataclasses do not support a pre-existing "
+ "'__table__' element",
+ ):
+
+ @registry.mapped_as_dataclass
+ class Foo:
+ __table__ = Table("foo", registry.metadata)
+ foo: Mapped[float]
+
def test_dataclass_exception_wrapped(self, dc_decl_base):
with expect_raises_message(
exc.InvalidRequestError,