Skip to content

Commit

Permalink
Work around Django 2.1 db router changes
Browse files Browse the repository at this point in the history
Changes from django/django#9360 caused more aggressive router lookups, which resulted in infinite recursion when attempting to save some new related objects.
  • Loading branch information
millerdev committed Jul 2, 2020
1 parent eadd947 commit 0372f63
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions corehq/sql_db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ def partition_attr(self):

@property
def partition_value(self):
if self.partition_attr not in self.__dict__ and self.pk is None:
# prevent infinite recursion in db router
raise AttributeError(
f"unknown partition value for unsaved {type(self).__name__}")
return getattr(self, self.partition_attr)

@property
Expand Down
5 changes: 4 additions & 1 deletion corehq/sql_db/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ def get_db_for_partitioned_model(model, hints):
raise Exception(f'Unable to perform routing, multiple hints provided: {hints}')

if HINT_INSTANCE in hints:
partition_value = getattr(hints[HINT_INSTANCE], 'partition_value', None)
instance = hints[HINT_INSTANCE]
if instance._state.db is not None:
return instance._state.db
partition_value = getattr(instance, 'partition_value', None)
if partition_value is not None:
return get_db_alias_for_partitioned_doc(partition_value)
if hints.get(HINT_PLPROXY):
Expand Down

0 comments on commit 0372f63

Please sign in to comment.