Skip to content

Replace in-memory _relations with persisted reverse indexes#10

Merged
deucalioncodes merged 4 commits into
mainfrom
feat/reverse-index-relations
May 18, 2026
Merged

Replace in-memory _relations with persisted reverse indexes#10
deucalioncodes merged 4 commits into
mainfrom
feat/reverse-index-relations

Conversation

@deucalioncodes
Copy link
Copy Markdown
Member

Summary

  • Replaces the fragile in-memory _relations dict with persisted reverse indexes in stable storage, making relationship resolution (OneToMany, inverse OneToOne, ManyToMany) O(k) instead of O(max_id)
  • Eliminates the structural dependency on Entity.instances() for relationship access, which was causing IC instruction limit failures at scale (the demo simulator issue)
  • Adds comprehensive regression tests proving relationships resolve correctly after clearing all in-memory state (simulating canister upgrade/GC)

Resolves #9

Breaking Changes

  • OneToMany cannot be set directly; use ManyToOne on each child instead
  • Entity.add_relation / get_relations / remove_relation removed
  • Entity.instances() deprecated (still works with a DeprecationWarning)
  • RelationList removed; OneToMany / ManyToMany return plain lists
  • Serialization only includes owning-side FK references

Test plan

  • All 153 existing tests pass (excluding 8 pre-existing failures unrelated to this change)
  • 4 new regression tests in TestReverseIndexResolution specifically validate the fix
  • CI workflow passes

Made with Cursor

deucalioncodes and others added 4 commits May 18, 2026 11:05
Resolves #9. Relationship resolution (OneToMany, inverse OneToOne,
ManyToMany) now uses persisted reverse indexes in stable storage instead
of the fragile in-memory _relations dict. This makes relationship
lookups O(k) instead of O(max_id), robust across canister upgrades
and GC, and eliminates the need for Entity.instances() as a structural
dependency.

Breaking changes:
- OneToMany cannot be set directly; use ManyToOne on each child
- Entity.add_relation/get_relations/remove_relation removed
- Entity.instances() deprecated (still works with warning)
- RelationList removed; OneToMany/ManyToMany return plain lists
- Serialization only includes owning-side FK references

Co-authored-by: Cursor <cursoragent@cursor.com>
…rmatting

reverse_index_delete now checks key existence before removal, preventing
KeyError when deleting entities with empty ManyToMany relations.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@deucalioncodes deucalioncodes merged commit f38ad98 into main May 18, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace in-memory _relations with persisted reverse indexes for OneToMany/OneToOne/ManyToMany resolution

1 participant