From cdf413bda28f89766b06398882977936c147f0ad Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Fri, 16 Sep 2016 22:56:48 -0400 Subject: [PATCH] Disambiguate queryset extensions for ConfigurationModels This change disambiguates the use of `id` in the ConfigurationModelManager queryset extensions. This is required when specifying KEY_FIELDS where there is more than one key and at least one of them is a ForeignKey. The table must be specified in this case because the query extension may be appended to the WHERE clause of queries selecting `id` fields from multiple tables via an INNER JOIN clause. --- common/djangoapps/config_models/models.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/common/djangoapps/config_models/models.py b/common/djangoapps/config_models/models.py index ab429c701d..1eec190771 100644 --- a/common/djangoapps/config_models/models.py +++ b/common/djangoapps/config_models/models.py @@ -41,7 +41,10 @@ class ConfigurationModelManager(models.Manager): """ assert self.model.KEY_FIELDS != (), "Just use model.current() if there are no KEY_FIELDS" return self.get_queryset().extra( # pylint: disable=no-member - where=["id IN ({subquery})".format(subquery=self._current_ids_subquery())], + where=["{table_name}.id IN ({subquery})".format( + table_name=self.model._meta.db_table, # pylint: disable=protected-access + subquery=self._current_ids_subquery(), + )], select={'is_active': 1}, # This annotation is used by the admin changelist. sqlite requires '1', not 'True' ) @@ -53,11 +56,17 @@ class ConfigurationModelManager(models.Manager): if self.model.KEY_FIELDS: subquery = self._current_ids_subquery() return self.get_queryset().extra( # pylint: disable=no-member - select={'is_active': "id IN ({subquery})".format(subquery=subquery)} + select={'is_active': "{table_name}.id IN ({subquery})".format( + table_name=self.model._meta.db_table, # pylint: disable=protected-access + subquery=subquery, + )} ) else: return self.get_queryset().extra( # pylint: disable=no-member - select={'is_active': "id = {pk}".format(pk=self.model.current().pk)} + select={'is_active': "{table_name}.id = {pk}".format( + table_name=self.model._meta.db_table, # pylint: disable=protected-access + pk=self.model.current().pk, + )} )