You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It works with executing raw sql. But it cannot update by updating the object with the following error: StaleDataError: UPDATE statement on table 'table1' expected to update 1 row(s); 0 were matched.
Do anyone know why? Does sqlalchemy have issue with updating view?
Here is the test code:
fromsqlalchemy.ext.declarativeimportdeclarative_basefromsqlalchemyimportColumn, Integer, String, JSON, textfromsqlalchemyimportevent, create_enginefromsqlalchemy.ormimportsessionmakerfromsqlalchemy.ext.compilerimportcompiles# Use this to make the json column has affinity TextclassTEXTJSON(JSON):
pass@compiles(TEXTJSON, 'sqlite')defbi_c(element, compiler, **kw):
return"TEXTJSON"@compiles(TEXTJSON)defbi_c(element, compiler, **kw):
returncompiler.visit_JSON(element, **kw)
Base=declarative_base()
classTable1(Base):
__tablename__='table1'id=Column(Integer, primary_key=True, autoincrement=True)
name=Column(String, nullable=True, index=True)
data=Column(TEXTJSON, nullable=True)
def__repr__(self) ->str:
returnf"{self.__class__.__name__}(id={self.id})"# Create database and load extensionengine=create_engine('sqlite:///:memory:')
@event.listens_for(engine, "connect")defreceive_connect(connection, _):
connection.enable_load_extension(True)
connection.execute("SELECT load_extension('sqlite_zstd.dll')")
connection.enable_load_extension(False)
# Create table and sessionBase.metadata.create_all(engine)
make_session=sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Insert and confirm record can be added and updatedwithmake_session() assession:
session.add(Table1(name="record 1", data={"count": 0}))
session.commit()
withmake_session() assession:
record=session.query(Table1).first()
record.data= {"count": 1}
session.commit()
withmake_session() assession:
record=session.query(Table1).first()
assertrecord.data["count"] ==1# Make zstdwithmake_session() assession:
session.execute(text(''' SELECT zstd_enable_transparent('{"table": "table1", "column": "data", "compression_level": 19, "dict_chooser": "''a''"}') '''))
# Assert insert, insert is success.withmake_session() assession:
session.add(Table1(name="record 2", data={"count": 0}))
session.commit()
assertsession.query(Table1).count() ==2# Assert update, update is fail:withmake_session() assession:
record=session.query(Table1).first()
record.data= {"count": 2}
session.commit()
The text was updated successfully, but these errors were encountered:
It works with executing raw sql. But it cannot update by updating the object with the following error:
StaleDataError: UPDATE statement on table 'table1' expected to update 1 row(s); 0 were matched.
Do anyone know why? Does sqlalchemy have issue with updating view?
Here is the test code:
The text was updated successfully, but these errors were encountered: