Formula `lookup` function/Lookup fields are not always updated
Describe the problem
The formula value isn't always updated if it depends on a field in another table using the lookup
function. When a field value in the related table is updated, the value is updated properly. If you change the relationship in the related table it's not updated.
I think that something is going wrong with the field dependencies. If you have lookup field or a formula containing a lookup, it doesn’t add the link row field of the related table to the dependencies. In the video above, “Name of table” is a lookup to “Table” -> “Name”. The name field is a correctly registered as a dependency, but the “Demo” field is not.
Cause of the problem
When a link row field is created, a corresponding field is created in the other table. If a lookup field or formula is then constructed which depends via this link row field, changes to the corresponding related link field do not automatically update the lookup field. This is because the corresponding related link field is never added to the field dependency graph.
Steps to reproduce
Index: backend/tests/baserow/contrib/database/field/test_lookup_field_type.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/backend/tests/baserow/contrib/database/field/test_lookup_field_type.py b/backend/tests/baserow/contrib/database/field/test_lookup_field_type.py
--- a/backend/tests/baserow/contrib/database/field/test_lookup_field_type.py (revision fc15ee3ca4d6835b1311558700f7fdfb6bdac5d1)
+++ b/backend/tests/baserow/contrib/database/field/test_lookup_field_type.py (date 1650620296531)
@@ -1593,3 +1593,40 @@
assert LookupField.objects.count() == 0
assert FormulaField.objects.count() == 0
+
+
+@pytest.mark.django_db
+def test_updating_other_side_of_link_row_field_updates_lookups(
+ data_fixture, api_client
+):
+ user, token = data_fixture.create_user_and_token()
+ table_a, table_b, link_field = data_fixture.create_two_linked_tables(user=user)
+ id_mapping = {}
+
+ target_field = data_fixture.create_text_field(name="target", table=table_b)
+ table_a_model = table_a.get_model(attribute_names=True)
+ table_b_model = table_b.get_model(attribute_names=True)
+ row_1 = table_b_model.objects.create(primary="1", target="target 1")
+ row_2 = table_b_model.objects.create(primary="2", target="target 2")
+
+ row_a = table_a_model.objects.create(primary="a")
+ row_a.save()
+
+ lookup = FieldHandler().create_field(
+ user,
+ table_a,
+ "lookup",
+ name="lookup",
+ through_field_name="link",
+ target_field_name="target",
+ )
+
+ RowHandler().update_row(
+ user,
+ table_b,
+ row_1.id,
+ {f"{link_field.link_row_related_field.db_column}": [row_a.id]},
+ )
+
+ table_a_model = table_a.get_model(attribute_names=True)
+ assert table_a_model.objects.get(id=row_a.id).lookup == "target 1"
Actual result
The formula field is empty after updating the relationship in the related table.
Expected result
The formula field should be updated if a relationship is added or removed in the related table.
Environment
> 1.9.1