Back to snippets
sqlalchemy_continuum_model_versioning_quickstart.py
pythonThis quickstart demonstrates how to enable versioning for a model,
Agent Votes
0
0
sqlalchemy_continuum_model_versioning_quickstart.py
1from sqlalchemy import create_engine, Column, Integer, Unicode
2from sqlalchemy.orm import sessionmaker, declarative_base
3from sqlalchemy_continuum import make_versioned
4
5
6# 1. Initialize versioning
7make_versioned(user_cls=None)
8
9Base = declarative_base()
10
11# 2. Add __versioned__ to models you want to track
12class Article(Base):
13 __versioned__ = {}
14 __tablename__ = 'article'
15
16 id = Column(Integer, primary_key=True, autoincrement=True)
17 name = Column(Unicode(255))
18 content = Column(Unicode(255))
19
20# 3. Configure the engine and create tables
21engine = create_engine('sqlite:///')
22Base.metadata.create_all(engine)
23
24Session = sessionmaker(bind=engine)
25session = Session()
26
27# 4. Create an article (Version 1)
28article = Article(name='My first article', content='Some content')
29session.add(article)
30session.commit()
31
32# 5. Update the article (Version 2)
33article.name = 'My updated article'
34session.commit()
35
36# 6. Access historical versions
37print(article.versions[0].name) # 'My first article'
38print(article.versions[1].name) # 'My updated article'