Skip to content

Fix self-referential foreign key recursion#709

Merged
ascott18 merged 3 commits into
IntelliTect:mainfrom
immense:fix/self-referential-fk-recursion
May 12, 2026
Merged

Fix self-referential foreign key recursion#709
ascott18 merged 3 commits into
IntelliTect:mainfrom
immense:fix/self-referential-fk-recursion

Conversation

@dkattan
Copy link
Copy Markdown
Contributor

@dkattan dkattan commented May 6, 2026

Summary

  • separate direct foreign-key discovery from the shared-key fallback heuristics in PropertyViewModel
  • avoid recursive ForeignKeyProperty evaluation when scanning inverse/shared-key candidates
  • add regression coverage for self-referential and non-conventional navigation naming

Root cause

PropertyViewModel.ForeignKeyProperty could recurse indefinitely while trying to infer shared-key one-to-one relationships from navigation properties whose direct FK could not be resolved yet. Self-referential and cross-linked navigations made that especially easy to trigger.

While investigating against Immybot, the recursion showed up in cycles like:

  • Branding.OwnerTenant -> Tenant.OwnerTenant -> Tenant.OwnerTenant
  • SmtpConfig.OwnerTenant -> Tenant.SmtpConfig -> SmtpConfig.OwnerTenant
  • User.ImpersonatorUser -> User.CreatedByUser -> User.ImpersonatorUser

This change keeps the shared-key heuristics, but only allows them to inspect direct FK matches instead of recursively re-entering ForeignKeyProperty.

Validation

  • DOTNET_ROLL_FORWARD=Major dotnet test --project src/IntelliTect.Coalesce.Tests/IntelliTect.Coalesce.Tests.csproj --framework net10.0 --no-restore
  • linked Coalesce source into Immybot and ran the Coalesce CLI against ImmybotDbContext
  • generation no longer stack overflows; it now proceeds to regular model validation failures in Immybot's broad model

@ascott18
Copy link
Copy Markdown
Collaborator

ascott18 commented May 8, 2026

Hi @dkattan - thanks for the contribution. I see this is still a draft PR - was that intentional, or is it ready for a look?

Split direct foreign-key discovery from the shared-key fallback heuristics so self-referential and non-conventional navigation names don't recurse through ForeignKeyProperty while model validation is still trying to classify the relationship.

Add regression coverage for a self-referential navigation and for a consumer that targets that entity.

Validated with Coalesce tests on net10 and with Immybot's ImmybotDbContext, where generation now reaches normal model validation errors instead of stack overflowing.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dkattan dkattan force-pushed the fix/self-referential-fk-recursion branch from 1e89506 to 8ac6041 Compare May 9, 2026 12:59
@dkattan dkattan marked this pull request as ready for review May 9, 2026 12:59
@dkattan
Copy link
Copy Markdown
Contributor Author

dkattan commented May 9, 2026

Hi @dkattan - thanks for the contribution. I see this is still a draft PR - was that intentional, or is it ready for a look?

I just marked it as ready. I'm working on a bunch of other contributions at the moment, this was just the first. Really excited to have discovered this library. I have been trying unsuccessfully to stitch something like this together for years.

@ascott18 ascott18 merged commit 0d96768 into IntelliTect:main May 12, 2026
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.

2 participants