Skip to content

Support '__all__' in no_db_index to exclude all fields from indexing in the Historical Model #1492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Authors
- `Sridhar Marella <https://github.com/sridhar562345>`_
- `Mattia Fantoni <https://github.com/MattFanto>`_
- `Trent Holliday <https://github.com/trumpet2012>`_
- Raja Rehan Ahmed (`rajarehanahmed <https://github.com/rajarehanahmed>`_)

Background
==========
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Unreleased
----------

- Tests are no longer bundled in released wheels (gh-1478)
- ``no_db_index`` in RecordModels model now supports ```"__all__"`` in order to drop all
indices in the historical model (gh-1491)

3.9.0 (2025-01-26)
------------------
Expand Down
22 changes: 22 additions & 0 deletions docs/historical_model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,28 @@ And you don't want to create database index for ``question``, it is necessary to
history = HistoricalRecords(no_db_index=['question'])


A single field can also be passed as a string instead of a list:

.. code-block:: python

class PollWithExcludeFields(models.Model):
question = models.CharField(max_length=200, db_index=True)

history = HistoricalRecords(no_db_index='question')


It is also possible to exclude all fields from having database indices in the historical table
by using __all__:

.. code-block:: python

class PollWithExcludeFields(models.Model):
question = models.CharField(max_length=200, db_index=True)
published_at = models.DateTimeField(auto_now_add=True, db_index=True)

history = HistoricalRecords(no_db_index='__all__')


By default, django-simple-history keeps all indices. and even forces them on unique fields and relations.
WARNING: This will drop performance on historical lookups

Expand Down
7 changes: 5 additions & 2 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def _history_user_setter(historical_instance, user):
historical_instance.history_user_id = user.pk


ALL_FIELDS = "__all__"


class HistoricalRecords:
DEFAULT_MODEL_NAME_PREFIX = "Historical"

Expand Down Expand Up @@ -142,7 +145,7 @@ def __init__(
self.m2m_fields = m2m_fields
self.m2m_fields_model_field_name = m2m_fields_model_field_name

if isinstance(no_db_index, str):
if isinstance(no_db_index, str) and no_db_index != ALL_FIELDS:
no_db_index = [no_db_index]
self.no_db_index = no_db_index

Expand Down Expand Up @@ -402,7 +405,7 @@ def copy_fields(self, model):
transform_field(field)

# drop db index
if field.name in self.no_db_index:
if self.no_db_index == ALL_FIELDS or field.name in self.no_db_index:
field.db_index = False

fields[field.name] = field
Expand Down
9 changes: 9 additions & 0 deletions simple_history/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,15 @@ class ModelWithMultipleNoDBIndex(models.Model):
history = HistoricalRecords(no_db_index=["name", "fk", "other"])


class ModelWithAllNoDBIndex(models.Model):
name = models.CharField(max_length=15, db_index=True)
name_no_index = models.CharField(max_length=15)
fk = models.ForeignKey(
"Library", on_delete=models.CASCADE, null=True, related_name="+"
)
history = HistoricalRecords(no_db_index="__all__")


class TestOrganization(models.Model):
name = models.CharField(max_length=15, unique=True)

Expand Down
22 changes: 22 additions & 0 deletions simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
InheritedRestaurant,
Library,
ManyToManyModelOther,
ModelWithAllNoDBIndex,
ModelWithCustomAttrOneToOneField,
ModelWithExcludedManyToMany,
ModelWithFkToModelWithHistoryUsingBaseModelDb,
Expand Down Expand Up @@ -2711,6 +2712,27 @@ def test_unique_field_index(self):
self.assertTrue(self.history_model._meta.get_field("name_keeps_index").db_index)


class ModelWithAllNoDBIndexTest(TestCase):
def setUp(self):
self.model = ModelWithAllNoDBIndex
self.history_model = self.model.history.model

def test_field_indices(self):
"""
All the indexed fields in the original model should be
non-indexed in the history model.
"""
for field in ["name", "fk"]:
# dropped index
self.assertTrue(self.model._meta.get_field(field).db_index)
self.assertFalse(self.history_model._meta.get_field(field).db_index)

# no index
no_index = "name_no_index"
self.assertFalse(self.model._meta.get_field(no_index).db_index)
self.assertFalse(self.history_model._meta.get_field(no_index).db_index)


class HistoricForeignKeyTest(TestCase):
"""
Tests chasing foreign keys across time points naturally with
Expand Down