sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

SQLModel inheritance breaks sqlalchemy mutation tracking

Open jtpavlock opened this issue 2 years ago • 4 comments

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

jtpavlock avatar Oct 16 '22 21:10 jtpavlock

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"} ?

blerrgh avatar May 15 '23 19:05 blerrgh

Any update here, A big blocker before I can migrate from sqlalchemy+pydantic to sqlmodel.

jyotirmay123 avatar Dec 05 '23 13:12 jyotirmay123

Also a blocker here, commenting to stay in the loop!

alexpotv avatar Jun 16 '24 18:06 alexpotv

yep, same here, blocker.

pwinged avatar Aug 18 '24 01:08 pwinged