corridor / sqlalchemy-history

Library to keep track of changes in SQLAlchemy.
Other
32 stars 24 forks source link

sqlalchemy v2.0.36 MappedAsDataclass #146

Open benedikt-bartscher opened 1 month ago

benedikt-bartscher commented 1 month ago

sqlalchemy introduced a new check/validation which breaks sqlalchemy-history compability with MappedAsDataclass

from sqlalchemy.orm import DeclarativeBase, Mapped, MappedAsDataclass, mapped_column
from sqlalchemy_history import make_versioned

make_versioned(user_cls="")

class Base(MappedAsDataclass, DeclarativeBase):
    pass

class MyModel(Base):
    __versioned__ = {}
    __tablename__ = "my_model"
    id: Mapped[int] = mapped_column(primary_key=True)

_ = MyModel(id=1)
sqlalchemy.exc.InvalidRequestError: Class <class 'sqlalchemy_history.model_builder.MyModelVersion'> already defines a '__table__'. ORM Annotated Dataclasses do not support a pre-existing '__table__' element

upstream refs: https://github.com/sqlalchemy/sqlalchemy/issues/11973 https://github.com/sqlalchemy/sqlalchemy/commit/270b46cef3043d0e675ccb72b1e3a590f835dd4b

AbdealiLoKo commented 1 month ago

Hm, I havent tried MappedAsDataclass yet - do you have any experience or suggestions on how to get this to work ? PRs are welcome !

abulvenz commented 4 weeks ago

Yes, I can confirm this.

benedikt-bartscher commented 4 weeks ago

Hm, I havent tried MappedAsDataclass yet - do you have any experience or suggestions on how to get this to work ? PRs are welcome !

Not really, i do not have looked into the sqlalchemy-file internals yet, but maybe i will in the next days