sqlmodel
sqlmodel copied to clipboard
SQLModel inheritance breaks sqlalchemy mutation tracking
First Check
- [X] I added a very descriptive title to this issue.
- [X] I used the GitHub search to find a similar issue and didn't find it.
- [X] I searched the SQLModel documentation, with the integrated search.
- [X] I already searched in Google "How to X in SQLModel" and didn't find any information.
- [X] I already read and followed all the tutorial in the docs and didn't find an answer.
- [X] I already checked if it is not related to SQLModel but to Pydantic.
- [X] I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
- [X] I commit to help with one of those options 👆
Example Code
#!/usr/bin/env python3
from typing import Optional, Any
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.mutable import MutableDict, MutableSet
from sqlalchemy.orm import sessionmaker
from sqlalchemy import JSON, Column
from sqlmodel import SQLModel, Field, create_engine
Session = sessionmaker()
Base = declarative_base()
class BadTrackBase(SQLModel):
custom: dict[str, Any] = Field(
sa_column=Column(MutableDict.as_mutable(JSON(none_as_null=True))), default={}
)
class BadTrack(BadTrackBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
class Track(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
custom: dict[str, Any] = Field(
sa_column=Column(MutableDict.as_mutable(JSON(none_as_null=True))), default={}
)
engine = create_engine("sqlite:///:memory:")
Session.configure(bind=engine)
SQLModel.metadata.create_all(engine)
session = Session()
good_track = Track(id=1, custom={"test": "good"})
bad_track = BadTrack(id=1, custom={"test": "bad"})
session.add(good_track)
session.add(bad_track)
session.commit()
good_track.custom["test"] = "changed"
bad_track.custom["test"] = "changed"
assert good_track in session.dirty
assert bad_track in session.dirty
Description
When setting a field to one of the fields supported by sqlalchemy mutation tracking, it seems the mutation tracking isn't working as intended. I attached the above code sample to illustrate what I mean. The assertions on the bottom of the script should pass under normal circumstance.
Operating System
Linux
Operating System Details
No response
SQLModel Version
0.08
Python Version
3.9.13
Additional Context
No response
I have the same issue in SQLModel 0.08 and it's kind of a big deal. Is there a workaround for this besides re-assigning the entire dict i.e. good_track.custom = {"test": changed"}
?
Any update here, A big blocker before I can migrate from sqlalchemy+pydantic to sqlmodel.
Also a blocker here, commenting to stay in the loop!
yep, same here, blocker.