Skip to content

refactor(relations): rename BackRelationship to BackRef and add Field(back_ref=True)#4

Merged
Ox54 merged 5 commits intomainfrom
feat/back-ref
Feb 16, 2026
Merged

refactor(relations): rename BackRelationship to BackRef and add Field(back_ref=True)#4
Ox54 merged 5 commits intomainfrom
feat/back-ref

Conversation

@Ox54
Copy link
Contributor

@Ox54 Ox54 commented Feb 14, 2026

Description

Renames the reverse-relationship marker from BackRelationship to BackRef and adds an alternative way to declare back-refs via Field(back_ref=True), so users can keep a plain type like list["Post"] | None while still marking the field as a reverse relation. The shorter name and optional back_ref flag improve ergonomics and type clarity.

Changes

  • Rename BackRelationship to BackRef across the codebase (query builder, public API, metaclass, relations resolution, tests, demo, docs).
  • Add back_ref: bool to Field(); reorder Ferro-specific params (primary_key, autoincrement, unique, index, back_ref) before Pydantic params in overloads and implementation.
  • In the model metaclass, detect back-refs via type (BackRef[...]) or via Field(back_ref=True) (default or in Annotated); raise TypeError if both are used on the same field.
  • Update docs: relations (BackRef + Field(back_ref=True) examples), fields (back_ref param table), API reference (query-builder BackRef), and extra.css (parameter table column nowrap).
  • Add integration tests for Field(default=None, back_ref=True), Annotated[..., Field(back_ref=True)], and the “both BackRef and back_ref=True” error.

Bridge and Schema Impact

  • No Rust/Python bridge changes
  • Python model/schema changed
  • Rust core or SQL generation changed
  • src/ferro/_core.pyi updated (if needed)
  • Integration test added first for new behavior

Migration / Breaking Changes

  • No breaking changes
  • Breaking changes included (details below)

Impact: Any use of BackRelationship must be updated to BackRef (same semantics). Optional: reverse relations can be declared with Field(back_ref=True) and a plain type instead of BackRef[...].

Upgrade: Replace BackRelationship with BackRef in imports and type annotations; no other code changes required for existing back-refs.

Documentation and Changelog

  • No docs update needed
  • Docs updated (README/docs/inline docs)
  • Changelog entry needed

Related Issues

Ox54 added 4 commits February 14, 2026 12:45
- Updated references from BackRelationship to BackRef across the codebase for consistency.
- Added new parameters (primary_key, autoincrement, unique, index, back_ref) to the Field function to enhance field definition capabilities.
- Improved back-reference handling in the metaclass to prevent conflicts between BackRef and Field(back_ref=True).
- Replaced instances of BackRelationship with BackRef in the documentation for clarity.
- Added back_ref parameter to Field documentation to improve understanding of reverse relationships.
- Updated examples to reflect the new usage of BackRef and Field(back_ref=True).
- Enhanced CSS to prevent parameter table columns from wrapping for better readability.
- Replaced all instances of BackRelationship with BackRef in the test suite for consistency with recent refactoring.
- Adjusted related test assertions to reflect the new BackRef usage.
- Added new tests to validate the behavior of back references using Field(back_ref=True) in various scenarios.
- Replaced instances of BackRelationship with BackRef in demo_queries.py for consistency with recent refactoring.
- Adjusted the products and actors fields in the Category and Movie models to reflect the new BackRef usage.
@Ox54 Ox54 self-assigned this Feb 14, 2026
@Ox54 Ox54 merged commit b3c2cde into main Feb 16, 2026
6 checks passed
@Ox54 Ox54 deleted the feat/back-ref branch February 16, 2026 13:14
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.

1 participant