diff --git a/netbox_branching/database.py b/netbox_branching/database.py index 33916e7..df4bff2 100644 --- a/netbox_branching/database.py +++ b/netbox_branching/database.py @@ -17,6 +17,9 @@ class BranchAwareRouter: """ connection_prefix = 'schema_' + def _get_connection(self, branch): + return f'{self.connection_prefix}{branch.schema_name}' + def _get_db(self, model, **hints): # Warn & exit if branching support has not yet been initialized if 'branching' not in registry['model_features']: @@ -30,9 +33,16 @@ def _get_db(self, model, **hints): # Return the schema for the active branch (if any) if branch := active_branch.get(): - return f'{self.connection_prefix}{branch.schema_name}' + return self._get_connection(branch) def db_for_read(self, model, **hints): + + # Always use the active branch (if any) when retrieving changelog records + if model._meta.label == 'core.ObjectChange': + if branch := active_branch.get(): + return self._get_connection(branch) + return + return self._get_db(model, **hints) def db_for_write(self, model, **hints):