Skip to content

Commit 5fe8859

Browse files
authored
fix: guard stale rollup field relations (baserow#5215)
1 parent 5b7a0d9 commit 5fe8859

2 files changed

Lines changed: 35 additions & 10 deletions

File tree

backend/src/baserow/contrib/database/fields/models.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from django.contrib.contenttypes.models import ContentType
55
from django.contrib.postgres.fields import ArrayField
6+
from django.core.exceptions import ObjectDoesNotExist
67
from django.core.validators import MaxValueValidator, MinValueValidator
78
from django.db import models
89
from django.utils.functional import cached_property
@@ -809,10 +810,15 @@ def save(self, *args, **kwargs):
809810
from baserow.contrib.database.formula.ast.function_defs import BaserowCount
810811
from baserow.contrib.database.formula.ast.tree import BaserowFieldReference
811812

812-
field_reference = BaserowFieldReference(
813-
getattr(self.through_field, "name", ""), None, None
814-
)
815-
self.formula = f"{BaserowCount.type}({field_reference})"
813+
try:
814+
through_field = getattr(self, "through_field")
815+
field_name = getattr(through_field, "name", "")
816+
except ObjectDoesNotExist:
817+
field_name = "'invalid through'"
818+
819+
field_ref = BaserowFieldReference(field_name, None, None)
820+
self.formula = f"{BaserowCount.type}({field_ref})"
821+
816822
super().save(*args, **kwargs)
817823

818824
def __str__(self):
@@ -852,13 +858,23 @@ def save(self, *args, **kwargs):
852858
formula_function_registry,
853859
)
854860

861+
try:
862+
through_field = getattr(self, "through_field")
863+
through_name = getattr(through_field, "name", "")
864+
except ObjectDoesNotExist:
865+
self.through_field = None
866+
through_name = "'invalid through'"
867+
868+
try:
869+
target_field = getattr(self, "target_field")
870+
target_name = getattr(target_field, "name", "")
871+
except ObjectDoesNotExist:
872+
self.target_field = None
873+
target_name = "'invalid target'"
874+
855875
formula_function = formula_function_registry.get(self.rollup_function)
856-
field_reference = BaserowFieldReference(
857-
getattr(self.through_field, "name", ""),
858-
getattr(self.target_field, "name", ""),
859-
None,
860-
)
861-
self.formula = f"{formula_function.type}({field_reference})"
876+
field_ref = BaserowFieldReference(through_name, target_name, None)
877+
self.formula = f"{formula_function.type}({field_ref})"
862878
super().save(*args, **kwargs)
863879

864880
def __str__(self):
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"type": "bug",
3+
"message": "Fix rollup and count fields crashing when a stale in-memory relation points to a deleted field during formula recalculation.",
4+
"issue_origin": "github",
5+
"issue_number": 5214,
6+
"domain": "database",
7+
"bullet_points": [],
8+
"created_at": "2026-04-17"
9+
}

0 commit comments

Comments
 (0)