+2  A: 

Although I haven't used SQLAlchemy specifically, I can give you some general tips that can be easily implemented in any ORM:

  • Separate out the versioned item into two tables, say Document and DocumentVersion. Document stores information that will never change between versions, and DocumentVersion stores information that does change.
  • Give each DocumentVersion a "parent" reference. Make a foreign key to the same table, pointing to the previous version of the document.
  • Roll back to previous versions by updating a reference from Document to the "current" version. Don't delete versions from the bottom of the chain.
  • When they make newer versions after rolling back, it will create another branch of versions.

Example, create A, B, C, rollback to B, create D, E:

(A)
 |
(B)
 | \
(C) (D)
     |
    (E)
Christian Oudard
The allowing of branching is cool, but wouldn't it be really really hard to do merging down the road?
voyager
This is not like source control. From the user's perspective, you have totally deleted C. You will never need to merge it in. The only reason it is still around is as an audit trail, or in case you need to manually fish out a deleted item for some reason.
Christian Oudard