fix: Print clear error when fetching/editing a relationship on an id-less node

PH
2mo ago
+76-2

What

Fixes #1153.

Fetching or editing a cardinality-many relationship on a node with no ID (e.g. one built with client.create() but never save()d) produced confusing errors:

  • node.rel.fetch() → ValueError: At least one filter must be provided to get() — because fetch() re-queries the parent via client.get(id=self.node.id) with id=None.
  • node.rel.add(...) / remove(...) → Must call fetch() on RelationshipManager before editing members — but fetch() can't succeed either, so the two guards send the caller in a circle.

Change

fetch(), add(), and remove() (both RelationshipManager and RelationshipManagerSync) now detect the missing node ID up front and raise a clear UninitializedError that nudges the user to .save() first, via a shared _raise_missing_identifier() helper. When the node does have an ID, the existing "Must call fetch()" guidance is preserved.

The message is deliberately phrased around "no ID to look it up by / likely not saved yet" rather than asserting the node doesn't exist in Infrahub — at that point only a local attribute has been inspected, no server call has been made.

Tests

Added to tests/unit/sdk/test_node.py (async + sync):

  • test_fetch_relationship_without_node_id_raises_clear_error
  • test_edit_relationship_without_node_id_raises_clear_error
  • test_edit_relationship_with_node_id_still_requires_fetch (regression: id-bearing node still gets "call fetch()")

uv run invoke format lint-code clean; uv run pytest tests/unit/sdk/test_node.py → 228 passed.

Related

Sibling PR for #1152 (assigning to a cardinality-many relationship).


Summary by cubic

Show a clear error when fetching or editing a relationship on a node without an ID, guiding users to save the node first. Prevents the generic get() error and the circular “call fetch()” guidance.

  • Bug Fixes
    • In RelationshipManager and RelationshipManagerSync, fetch(), add(), and remove() now detect a missing node ID and raise UninitializedError with a hint to call .save() first.
    • Preserves the original “Must call fetch()” error when the node has an ID but the relationship isn’t loaded; added unit tests covering both cases.