Skip to content

fix(migrations): prune index_together in PostgresModelState.render (#286) - #299

Open
UsmanGhias wants to merge 1 commit into
SectorLabs:masterfrom
UsmanGhias:fix/prune-index-together-historical-state-286
Open

UsmanGhias wants to merge 1 commit into
SectorLabs:masterfrom
UsmanGhias:fix/prune-index-together-historical-state-286

Conversation

@UsmanGhias

Copy link
Copy Markdown

Summary

On Django 5.1+, index_together was removed from django.db.models.options.DEFAULT_NAMES. Django adapted its own ModelState.render (ticket #34856) to prune index_together before constructing the Meta class, allowing historical migrations containing index_together to render without errors.

In psqlextra, PostgresModelState.render() was constructing Meta directly from **self.options:

meta = type(
    "Meta",
    (),
    {"app_label": self.app_label, "apps": apps, **self.options},
)

When historical migrations for custom or partitioned models still contain index_together in their options, Django 5.1+ throws:

TypeError: 'class Meta' got invalid attribute(s): index_together

Changes

  1. In psqlextra/backend/migrations/state/model.py, prune index_together from meta_options prior to calling type("Meta", ...):
    meta_options = {**self.options} if self.options else {}
    meta_options.pop("index_together", None)
  2. Added test_partitioned_model_render_index_together in tests/test_partitioned_model_state.py asserting that historical partitioned model state with index_together renders cleanly without raising TypeError.

Closes #286

…ectorLabs#286)

On Django 5.1+, index_together was removed from allowed Model Meta
attributes. When rendering historical model state for partitioned or
custom models that still contain index_together in self.options, Django
raises a TypeError during Options.contribute_to_class.

Prune index_together from meta_options before constructing the Meta class,
matching upstream Django's ticket #34856 fix for ModelState.render.
Add unit test asserting clean rendering of state with index_together.

Closes SectorLabs#286
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Historical migrations with index_together crash on Django 5.1+ for partitioned models (TypeError: 'class Meta' got invalid attribute(s): index_together)

1 participant