Closes #21016: Add missing MPTT tree indexes #354

Closed
opened 2026-04-05 16:27:05 +02:00 by MrUnknownDE · 0 comments
Owner

Originally created by @jnovinger on 2/13/2026

Fixes: #21016

Upgrade django-mptt to 0.18.0 and add the missing composite (tree_id, lft) indexes to all MPTT models.

Root Cause

django-mptt dynamically adds indexes to _meta.indexes via its metaclass, but doesn't update _meta.original_attrs['indexes']. Django's migration autodetector only checks original_attrs, so it never detected the missing indexes for models without an explicit indexes declaration in their Meta class.

This is a known issue: django-mptt/django-mptt#682

Solution

Add an empty indexes = () tuple to each affected model's Meta class. This triggers Django's migration detection for the dynamically-added MPTT indexes. We cannot define the indexes explicitly because the MPTT fields (tree_id, lft) don't exist when the Meta class is evaluated.

Affected Models (11)

  • Region, SiteGroup, Location
  • DeviceRole, Platform
  • ModuleBay, InventoryItem, InventoryItemTemplate
  • TenantGroup, ContactGroup
  • WirelessLANGroup

Changes

  • Upgrade django-mptt 0.17.0 → 0.18.0
  • Add indexes = () with explanatory comment to 9 model Meta classes
  • 3 new migrations adding composite indexes to all 11 MPTT models
*Originally created by @jnovinger on 2/13/2026* ### Fixes: #21016 Upgrade django-mptt to 0.18.0 and add the missing composite `(tree_id, lft)` indexes to all MPTT models. ### Root Cause django-mptt dynamically adds indexes to `_meta.indexes` via its metaclass, but doesn't update `_meta.original_attrs['indexes']`. Django's migration autodetector only checks `original_attrs`, so it never detected the missing indexes for models without an explicit `indexes` declaration in their Meta class. This is a known issue: django-mptt/django-mptt#682 ### Solution Add an empty `indexes = ()` tuple to each affected model's Meta class. This triggers Django's migration detection for the dynamically-added MPTT indexes. We cannot define the indexes explicitly because the MPTT fields (`tree_id`, `lft`) don't exist when the Meta class is evaluated. ### Affected Models (11) - Region, SiteGroup, Location - DeviceRole, Platform - ModuleBay, InventoryItem, InventoryItemTemplate - TenantGroup, ContactGroup - WirelessLANGroup ### Changes - Upgrade django-mptt 0.17.0 → 0.18.0 - Add `indexes = ()` with explanatory comment to 9 model Meta classes - 3 new migrations adding composite indexes to all 11 MPTT models
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/netbox#354