Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLModel inheritance breaks sqlalchemy mutation tracking #468

Open
8 tasks done
jtpavlock opened this issue Oct 16, 2022 · 4 comments
Open
8 tasks done

SQLModel inheritance breaks sqlalchemy mutation tracking #468

jtpavlock opened this issue Oct 16, 2022 · 4 comments
Labels
investigate question Further information is requested

Comments

@jtpavlock
Copy link

jtpavlock commented Oct 16, 2022

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • 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 jtpavlock added the question Further information is requested label Oct 16, 2022
@blerrgh
Copy link

blerrgh commented May 15, 2023

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

@jyotirmay123
Copy link

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

@AAraKKe AAraKKe mentioned this issue Mar 18, 2024
9 tasks
@alexpotv
Copy link

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

@pwinged
Copy link

pwinged commented Aug 18, 2024

yep, same here, blocker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate question Further information is requested
Projects
None yet
Development

No branches or pull requests

6 participants