kvesteri / sqlalchemy-continuum

Versioning extension for SQLAlchemy.
BSD 3-Clause "New" or "Revised" License
575 stars 127 forks source link

If use sqlalchemy 'update' the object, the continuum versioned is not work. #253

Open hailong2018 opened 3 years ago

hailong2018 commented 3 years ago

` from sqlalchemy_continuum import make_versioned from sqlalchemy_continuum.plugins import FlaskPlugin from sqlalchemy.ext.declarative import declarative_base

def to_dict(self): return { c.name: getattr(self, c.name, None) for c in self.table.columns } Base = declarative_base() Base.to_dict = to_dict make_versioned(plugins=[FlaskPlugin()])

class Servers(Base): versioned = {} tablename = 'servers' id = Column(Integer, primary_key=True) hostname = Column(String(32), unique=True)

configure_mappers()

server = Servers.query.filter_by(id=3) server.update({'hostname': 'tj-xlh-sre-dr-xxxx'}) db_session.commit()

this not work for versioned

server = Servers.query.filter_by(id=3).first() server.hostname='tj-xlh-sre-dr-xxxx' db_session.commit()

it's work well.

` so, anyone can tell my why the sqlalchemy 'update' is not work for versioned? Many thanks!

cgearing commented 3 years ago

I've run into this in other cases.

In the first instance:

server = Servers.query.filter_by(id=3)
server.update({'hostname': 'tj-xlh-sre-dr-xxxx'})
db_session.commit()

You're emitting a bulk operation which SQLAlchemy-Continuum cannot track. See https://github.com/kvesteri/sqlalchemy-continuum/issues/174 Apparently switching to Native Versioning (which uses database triggers) will solve this problem.

Alternatively, loading the SQLAlchemy object then updating it will be tracked.