Audit Table / History Table

300 views
Skip to first unread message

LPG

unread,
Nov 19, 2012, 3:10:11 PM11/19/12
to sqlal...@googlegroups.com
I'm looking for details on implementing an audit table, either through sqla itself or in a way that plays nicely with sqla. It looks like there used to be an implementation of something like that at http://www.sqlalchemy.org/trac/wiki/UsageRecipes/LogVersions, does anyone know where that code lives now?

I figure there is probably a way to define the history table entirely in sqla, or perhaps preferably with db triggers. I'm using PostgreSQL / the trigger option is certainly available. If I go the trigger route ... is there a way to still have access to the history table object through sqla? I suppose I create a mapping to that table?

To illustrate the scenario, say I have a bank_account table:

bank_account
 - id
 - balance

And a history table for it:

bank_account_history
 - bid
 - timestamp
 - balance

Should the history table implementation be done in sqla or should it be a db trigger with a sqla mapping? Either way I want to read from the history table with SQLA. Any pointers appreciated!

Robert Forkel

unread,
Nov 19, 2012, 3:13:25 PM11/19/12
to sqlal...@googlegroups.com
http://docs.sqlalchemy.org/en/rel_0_8/orm/examples.html#versioned-objects
looks like what you may want.
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sqlalchemy/-/Ru8xYbuARNsJ.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to
> sqlalchemy+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.

LPG

unread,
Nov 19, 2012, 3:24:07 PM11/19/12
to sqlal...@googlegroups.com, xrot...@googlemail.com
Ahah excellent, thanks! Googling "sqlalchemy history table" / "sqlalchemy audit table" and searching the docs but those matches don't show up :P

Glad to have one way of doing it ... I don't think this uses triggers and I wonder if this can be optimized much by making use of triggers. If I happen to explore and implement that route I'll post the approach here for future searchers. Any pointers on a trigger implementation from anyone also appreciated.

Cheers
Reply all
Reply to author
Forward
0 new messages