litestar-org / litestar-org/polyfactory

Enhancement: Foreign Keys within hierarchical Model

Open
#868 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
1.5k
Forks
120
PR merge metrics
No merged PRs in 30d

Description

Summary

I have a hierarchical model in which some deeply nested values reference ID values in another part of the tree (as in IDREF in XML Schema). I would like to generate a consistent structure where every reference actually exists. If this were only one part with refs and another with IDs, I would probably find a solution with PostGenerated, but unfortunately, there are multiple places with ID definitions and multiple places referencing them, one branch containing even one kind of ID definition and at the same time references towards another branch with other IDs.

Basic Example
@dataclass
class X:
    x_id: int
    x_name: str

@dataclass
class Y:
    y_id: int
    xs_used: list[int]
    y_name: str

@dataclass
class DefinitionsOfX:
    xs: list[X]

@dataclass
class DefinitionsOfY:
    ys: list[Y]

@dataclass
class UsageOfY:
    info1: str
    info2: int
    current_y: int

@dataclass
class LargeTree:
    branch1: DefinitionsOfX
    branch2: DefinitionsOfY
    branch3: list[UsageOfY]

Here, LargeTree.branch3.*.current_y references LargeTree.branch2.ys.*.y_id and LargeTree.branch2.ys.*.xs_used.* references LargeTree.branch1.xs.*.x_id (with * indicating "every" or "some", I think you get the point). It could also involve a Mapping with the declared IDs as keys.

It would be great to have a way to specify this.

Drawbacks and Impact

Generation of fields would have to build a dependency graph and generate in the right order; if the dependency graph were to contain cycles, it would even necessitate a multi-pass strategy, I guess.

Unresolved questions

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the hierarchical dataclass example and the existing PostGenerated approach mentioned in the issue. Investigate how factory generation currently orders nested values and whether cross-branch references are represented anywhere. Done should include a defined way to declare these references, consistent generation for the example, and a clear strategy for dependency cycles.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.