Skip to content

fix Add support for Django reverse relationships for cross module #2071 - #5130

Open
asukaminato0721 wants to merge 3 commits into
meta:mainfrom
asukaminato0721:2071
Open

asukaminato0721 wants to merge 3 commits into
meta:mainfrom
asukaminato0721:2071

Conversation

@asukaminato0721

Copy link
Copy Markdown
Collaborator

Summary

Fixes #2071

By let user write these models down in configure.

  • I am a human, and I fully understand the code I am submitting.

Test Plan

add test

@meta-cla meta-cla Bot added the cla signed label Oct 7, 2026
@github-actions github-actions Bot added the size/l label Oct 7, 2026
@meta-codesync

meta-codesync Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D123799553. (Because this pull request was imported automatically, there will not be any future comments.)

@asukaminato0721

Copy link
Copy Markdown
Collaborator Author

todo: this will need manually add configure, is it possible to do some cold start?

@github-actions

This comment has been minimized.

@MorganBennetDev

Copy link
Copy Markdown

Not familiar with how the Rust code works, but will this support globs (src/*/models.py) or only explicit paths?

@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@rchen152

rchen152 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

@asukaminato0721 thanks for taking another stab at this. Sorry for never getting around to posting design ideas as I promised to on your last PR.

This direction looks promising to me, but AI review turned up a couple potentially blocking correctness issues (1, 2) and a couple more minor issues. I haven't vetted these carefully, so please feel free to push back on anything the AI got wrong:

  1. The ThreadState cache loses dependency edges and ignores per-module config. Only the module whose solve fills the cache records the dependency; models whose fields are computed on demand later in that solve hit the cache and record nothing, so their reverse fields can go stale, depending on solve order. In a project with nested configs, one sub-project's list can also leak into another's. answers.get already memoizes, so the fix is to delete the cache.
  2. Invalidation never fires. KeyDjangoRelations doesn't override Keyed::try_to_anykey in pyrefly/lib/binding/binding.rs, so diffing a module's results always skips it. Editing a listed module's relations is never reported to the modules that read them, so in the IDE every cross-module reverse field would go stale after an edit, cache or no cache. This needs a fix and an incremental test.
  3. (ux weakness) Bad config entries are dropped silently. An entry that can't be resolved, such as a typo, gives no warning.
  4. (nit) The merge code is clunky. The four-arm match merging the two Options can be a single chain(...).reduce(ClassSynthesizedFields::combine).

@samwgoldman I'd love to get your thoughts on this change, since you're much more familiar with the state machinery.

@rchen152 rchen152 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Forgot to submit a review XD)

Reverse relationship discovery needs an explicit list of model modules to support lazy loading. Add the configuration contract, validation, and reference documentation for that list.
Load explicitly configured model modules when solving reverse relationships and record their dependencies. This supports cross-module relations with lazy module loading and invalidation, without scanning all loaded modules.
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for Django reverse relationships

3 participants