MemberJunction / MemberJunction/MJ

CodeGen generates duplicate relationship fields when LLM soft FK matches existing relationship

Open
#3,160 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
TSQL
Stars
29
Forks
6
Avg merge
2d 1h
Merged PRs (30d)
323

Description

## Summary

When CodeGen's LLM-based field analysis (`advanced_generation.ts`) detects a soft FK relationship that already exists (either from a real FK or a prior soft FK), the server-side TypeGraphQL code generation (`generated.ts`) emits the relationship array field **twice**, causing TypeScript compilation errors.

## Reproduction

1. Have an entity with fields like `MemberID`, `ProfileID`, `GLCode` that reference other entities
2. CodeGen's LLM analysis detects these as soft FKs and sets `IsSoftForeignKey = 1`, `RelatedEntityID`, `RelatedEntityFieldName`
3. The relationship also gets recorded in the `EntityRelationship` table (or was already there)
4. CodeGen generates the TypeGraphQL class with the relationship array field from **both** sources
5. Build fails with `TS2300: Duplicate identifier` errors

## Example Errors

```
generated.ts(10722,5): error TS2300: Duplicate identifier 'ymEventTickets_GLCodeArray'.
generated.ts(11048,5): error TS2300: Duplicate identifier 'ymMemberGroups_GroupIDArray'.
generated.ts(13269,5): error TS2300: Duplicate identifier 'ymMembers_MemberTypeCodeArray'.
generated.ts(13894,5): error TS2300: Duplicate identifier 'ymDuesTransactions_MemberIDArray'.
generated.ts(13897,5): error TS2300: Duplicate identifier 'ymMemberGroups_MemberIDArray'.
generated.ts(13900,5): error TS2300: Duplicate identifier 'ymEventRegistrations_ProfileIDArray'.
generated.ts(13906,5): error TS2300: Duplicate identifier 'ymInvoiceItems_WebSiteMemberIDArray'.
```

## Additional Issue

The LLM re-detects and re-applies these soft FKs on every CodeGen run, even after manual reversion. There doesn't appear to be a way to suppress the LLM analysis for specific fields (e.g., via `AutoUpdateRelatedEntityInfo = 0`), so the duplicates recur on every run.

## Expected Behavior

1. CodeGen's TypeGraphQL generation should deduplicate relationship fields — if both a soft FK and an EntityRelationship point to the same target entity via the same field, only one array field should be emitted
2. Setting `AutoUpdateRelatedEntityInfo = 0` on an EntityField should prevent CodeGen's LLM from overwriting the field's relationship metadata

## Affected Files

- `packages/CodeGenLib/src/Misc/advanced_generation.ts` — LLM soft FK detection
- `packages/MJAPI/src/generated/generated.ts` — TypeGraphQL class generation (output)
- Server-side codegen template that emits relationship array fields

Contributor guide

Open the contributing guide

Research direction

Start by reading packages/CodeGenLib/src/Misc/advanced_generation.ts and tracing how its relationship metadata reaches the server-side TypeGraphQL template that produces packages/MJAPI/src/generated/generated.ts. Compare the soft-FK and EntityRelationship paths, then verify the generated output no longer contains duplicate fields and that AutoUpdateRelatedEntityInfo = 0 prevents the stated metadata update.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, backend, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.