fix: Print clear error when fetching/editing a relationship on an id-less node
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()— becausefetch()re-queries the parent viaclient.get(id=self.node.id)withid=None.node.rel.add(...)/remove(...)→Must call fetch() on RelationshipManager before editing members— butfetch()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_errortest_edit_relationship_without_node_id_raises_clear_errortest_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
RelationshipManagerandRelationshipManagerSync,fetch(),add(), andremove()now detect a missing node ID and raiseUninitializedErrorwith 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.
- In