Skip to content

fix(infrahub): fetch a stored peer again when it lacks a required relationship - #462

Draft
pcDamasceno wants to merge 1 commit into
mainfrom
fix/peer-missing-required-relationship
Draft

pcDamasceno wants to merge 1 commit into
mainfrom
fix/peer-missing-required-relationship

Conversation

@pcDamasceno

@pcDamasceno pcDamasceno commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #461.

Bug

Loading the Infrahub side of a diff or sync failed with Cannot build unique_id for peer LocationRack[...] (relationship DcimDevice.location ...): missing identifier key(s) ['site'], although the mapping declares site and the rack has its site in Infrahub. --continue-on-error only skipped the relationship, so devices lost their location.

model_loader loads a kind with client.all(..., include=..., populate_store=True). When include names a relationship, the SDK also builds a node for each peer and stores it. DcimDevice.location points to the generic LocationHosting, so that node carries only what the generic declares, without site, and it replaces the rack loaded earlier in the store. resolve_peer_node fetched a stored peer again only when a required attribute was empty, so the partial rack was used.

Circumstances

A peer whose identifiers include a relationship that a generic does not declare, reached through that generic by a kind loaded after it. Here LocationRack (identifiers site, name) through DcimDevice.location.

How it was caught

Syncing NetBox racks and devices into Infrahub 1.12.5 with the dcim schemas and the rack extension of the schema library.

client.all(kind="LocationRack", include=["name"], populate_store=True)
client.store.get(key=rack_id, kind="LocationRack").site.id   # '<site id>'
client.all(kind="DcimDevice", include=["location"], populate_store=True)
client.store.get(key=rack_id, kind="LocationRack").site.id   # None

Fix

The completeness check in resolve_peer_node (renamed _node_is_complete) also treats a stored peer as incomplete when a required cardinality-one relationship holds no id, so the peer is fetched again by its concrete kind. Before, resolve_peer_node returned the partial rack with site.id == None. After, it returns the rack with its site. The cost is one fetch per such peer each time a kind that points to it is loaded.

Two tests cover a stored peer with and without its required relationship. The fake relationship schema in test_infrahub_node_to_diffsync.py gains the optional field that real relationship schemas carry.

No user-visible changes: no CLI flag or config key changes.

Not covered

Test plan

  • pytest tests/adapters: 226 passed, 3 skipped.
  • ruff format, ruff check and ty check clean on the changed files.
  • Replayed the failing load order against a local Infrahub 1.12.5: resolve_peer_node now returns the rack with its site.
  • The local rumdl hook was skipped for the commit only because it lints the whole working tree, including an untracked local folder. An export of the committed tree passes rumdl with no issues.

🤖 Generated with Claude Code

View guided diff Turn on auto-fix

…ationship

Fixes #461.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pcDamasceno pcDamasceno added type/bug Something isn't working as expected changes/patch Bump patch version (bug fix) labels Dec 3, 2026
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying infrahub-sync with  Cloudflare Pages  Cloudflare Pages

Latest commit: 3c3f892
Status: ✅  Deploy successful!
Preview URL: https://fb333612.infrahub-sync.pages.dev
Branch Preview URL: https://fix-peer-missing-required-re.infrahub-sync.pages.dev

View logs

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

Labels

changes/patch Bump patch version (bug fix) type/bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

adapter: loading Infrahub fails with "Cannot build unique_id for peer" when the peer is reached through a generic

1 participant