Skip to content

Not able to use MappedAsDataclass on a versioned table #375

Open

Description

@remcovaes

sqlalchemy-continuum, does not seem to work when using MappedAsDataclass.

See the following code:

from sqlalchemy.orm import DeclarativeBase, Mapped, MappedAsDataclass, Session, mapped_column

from sqlalchemy import Integer, String, create_engine
from sqlalchemy_continuum import make_versioned

class Base(MappedAsDataclass, DeclarativeBase):
  pass

make_versioned(user_cls=None)

engine = create_engine("sqlite://")

class Names(Base):
    __tablename__ = "names"
    __versioned__ = {}

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True, init=False)
    name: Mapped[str] = mapped_column(String, nullable=False)

Base.metadata.create_all(engine)

with Session(engine) as session:
    new_name = Names(name="example")
    session.add(new_name)
    session.commit()
    print("committed")
    name = session.query(Names).one()
    print(name.name)

Running this generates the following error:

sqlalchemy.exc.InvalidRequestError: Class <class 'sqlalchemy_continuum.model_builder.NamesVersion'> already defines a '__table__'. ORM Annotated Dataclasses do not support a pre-existing '__table__' element

Removing the versioned prop solves the issue, but then there is no version table. Also, removing the MappedAsDataclass, it works, but then there is no dataclass.

Is there a chance this would get fixed in the future?

Activity

  1. MrTrick commented on Feb 2, 2026

    @MrTrick

    As discussed in corridor/sqlalchemy-history#146 , the same workaround seems to work also in sqlalchemy-continuum:

    class Base(DeclarativeBase):
        """Base class of everything"""
        pass
    
    class DataclassBase(Base, MappedAsDataclass):
        """Base class for all your normal dataclass models"""
        __abstract__ = True
    
    # Versioned classes descend from Base, which is *not* a MappedAsDataclass
    make_versioned(options={"base_classes": (Base)})
    
    class MyStuff(DataclassBase): 
        """One of your models"""
        __tablename__ = "stuff"
        __versioned__ = {}
    
    ...etc.
  2. added
    pinned
    on Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pinned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions