Skip to content

[bug] Tree fix - #9979

Merged
SchrodingersGat merged 53 commits into
inventree:masterfrom
SchrodingersGat:tree-fix
Jul 13, 2025
Merged

SchrodingersGat merged 53 commits into
inventree:masterfrom
SchrodingersGat:tree-fix

Conversation

@SchrodingersGat

@SchrodingersGat SchrodingersGat commented Jul 8, 2025 •

Copy link
Copy Markdown
Member

This is a significant bug which was discovered as part of general hardening of tests for the upcoming 1.0.0 release

Background

We make use of the django-mptt library throughout the code, to realize a database-efficient "tree" structure for the following models:

  • PartCategory
  • Part
  • StockLocation
  • StockItem
  • Build

Of these, the PartCategory and StockLocation models have special consideration (via the InvenTreeTree model) which handles node deletion and re-parenting of child nodes.

The other models (Part, StockItem and Build) inherit directly from the MPTTModel class, without this mixin shim.

However - it turns out that by default, django-mptt does not handle tree rebuilds correctly when a node is deleted from the tree (at least, not in the way that we need it to).

So, as per the current code base, if a StockItem (or Part or Build) is deleted, and it has child nodes attached, those nodes are effectively orphaned, as they are not correctly re-parented to the upstream node. As these child nodes retain the same tree_id value, this creates a corrupted tree, which requires a full rebuild of the model structure (which can be extremely expensive for a large dataset).

Changes

This PR removes the direct inheritance from the MPTTModel class - instead ensuring that all our "tree" structures inherit from the InvenTreeTree class - which correctly handles node deletion, re-parenting, etc.

Additionally it adds a significant amount of unit test code to ensure that tree operations are sufficiently covered for all model types, to ensure we do not get a regression here.

To achieve this, the InvenTreeTree model had to be refactored somewhat, and some of the functionality has been split out into another mixin which is relevant only for the StockLocation and PartCategory models.

There are no migrations or changes to the API.

Backporting

If it can be backported cleanly...

TODO

  • Update Build model
  • Update StockItem model
  • Update Part model
  • Ensure all unit tests run

@SchrodingersGat SchrodingersGat added this to the 1.0.0 milestone Jul 8, 2025
@SchrodingersGat SchrodingersGat added bug Identifies a bug which needs to be addressed part Related to Part models stock Related to Stock models backport Apply this label to a PR to enable auto-backport action full-run Always do a full QC CI run backport-to-0.17.x labels Jul 8, 2025
@netlify

netlify Bot commented Jul 8, 2025 •

Copy link
Copy Markdown

✅ Deploy Preview for inventree-web-pui-preview canceled.

Name Link
🔨 Latest commit 057cf34
🔍 Latest deploy log https://app.netlify.com/projects/inventree-web-pui-preview/deploys/68737c9d599c1f00088098d9

@SchrodingersGat
SchrodingersGat requested a review from matmair July 8, 2025 07:12
@codecov

codecov Bot commented Jul 8, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.30769% with 24 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.85%. Comparing base (a2e06d1) to head (057cf34).
⚠️ Report is 268 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9979      +/-   ##
==========================================
+ Coverage   86.76%   86.85%   +0.09%     
==========================================
  Files        1244     1244              
  Lines       54850    55180     +330     
  Branches     2106     2106              
==========================================
+ Hits        47588    47928     +340     
+ Misses       6744     6734      -10     
  Partials      518      518              
Flag Coverage Δ
backend 88.61% <96.30%> (+0.09%) ⬆️
migrations 42.84% <10.30%> (-0.31%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Backend Apps 91.27% <98.30%> (+0.08%) ⬆️
Backend General 92.43% <ø> (+0.02%) ⬆️
Frontend 70.14% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@matmair

matmair commented Jul 8, 2025

Copy link
Copy Markdown
Member

Looking interesting @SchrodingersGat; can I review once it is fully done?

@SchrodingersGat

Copy link
Copy Markdown
Member Author

@matmair I'll leave this un-merged until you have had a change to properly review.

@SchrodingersGat
SchrodingersGat merged commit 22218fd into inventree:master Jul 13, 2025
@SchrodingersGat
SchrodingersGat deleted the tree-fix branch July 13, 2025 11:27
@github-actions

Copy link
Copy Markdown
Contributor

💔 All backports failed

Status Branch Result
❌ 0.17.x Backport failed because of merge conflicts

Manual backport

To create the backport manually run:

backport --pr 9979

Questions ?

Please refer to the Backport tool documentation and see the Github Action logs for details

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

Labels

backport Apply this label to a PR to enable auto-backport action bug Identifies a bug which needs to be addressed CI CI / unit testing ecosystem full-run Always do a full QC CI run part Related to Part models stock Related to Stock models

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants