On Tuesday, 25 August 2015 11:23:51 UTC+10, Alex Fraser wrote: > > Is there a declarative way to add foreign keys to the history table when > using history_meta.py? In the app I'm making the user can view old versions > of a document, and I want to make sure e.g. the user that created the old > version can't be deleted while their old versions exist. >
For what it's worth, we have solved this by checking for a 'version' flag in the info attribute of foreign key constraints: diff --git a/history_meta.py b/history_meta.py index f1e308e..da72f27 100644 --- a/history_meta.py +++ b/history_meta.py @@ -117,6 +117,16 @@ def _history_mapper(local_mapper): *cols, schema=local_mapper.local_table.schema ) + + for fk in local_mapper.local_table.foreign_key_constraints: + log.debug( + "Duplicating foreign key for history table: %s, fk: %s", + local_mapper.local_table, fk) + if 'version' in fk.info and fk.info['version']: + fk_new = fk.copy() + fk_new.info['history_copy'] = fk + table.append_constraint(fk_new) + else: # single table inheritance. take any additional columns that may have # been added and add them to the history table. Let me know if this is too dodgy :) Cheers, Alex -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.