Skip to content

Fix composite FK column mapping in generated from methods (#148)#149

Merged
oyvindberg merged 3 commits intomainfrom
fix/issue148-fk-column-mapping
Jul 8, 2025
Merged

Fix composite FK column mapping in generated from methods (#148)#149
oyvindberg merged 3 commits intomainfrom
fix/issue148-fk-column-mapping

Conversation

@oyvindberg
Copy link
Copy Markdown
Collaborator

@oyvindberg oyvindberg commented Jul 8, 2025

Summary

Fixes #148 - Fixed incorrect column mapping in composite foreign key from methods where ColsFromFk was simply zipping columns by position instead of matching them correctly based on the FK relationship.

Problem

When generating from methods in composite ID companion objects, the column mapping from foreign key composite IDs was incorrect. For example:

Before (incorrect):

utdanningsmulighetKode = TestUtdanningstilbudId.organisasjonskode

After (correct):

utdanningsmulighetKode = TestUtdanningstilbudId.utdanningsmulighetKode

Root Cause

In FkAnalysis.scala, the ColsFromFk.colPairs method was using otherCompositeId.cols.toList.zip(thisColumns) which matched columns by position, not by their actual FK relationship.

Solution

  • Added candidateFk parameter to ColsFromFk to access FK constraint information
  • Modified colPairs logic to map columns based on FK constraint relationship:
    • Find position of each column in candidateFk.thisFk.cols
    • Get corresponding column name from candidateFk.thisFk.otherCols
    • Match with correct column in otherCompositeId.cols by name
  • Added comprehensive test suite with 4 test cases covering different scenarios (different column names, identical names, reordered columns)

Changes

  • Core fix: typo/src/scala/typo/internal/FkAnalysis.scala
  • Test coverage: tests/src/scala/typo/internal/FkAnalysisTest.scala
  • Generated code: Updated generated files showing correct column mappings
  • Documentation: Updated CLAUDE.md with general development workflow

🤖 Generated with Claude Code

oyvindberg and others added 3 commits July 8, 2025 18:05
- Created issue148.sql with minimal reproduction case
- Added test tables for the foreign key mapping issue
- Updated install.sh to include the new SQL file
- Generated code showing the FK mapping problem

The issue: When generating TestInsert code for composite foreign keys,
the generated code incorrectly maps FK columns.

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
Fixed incorrect column mapping in composite foreign key `from` methods.
The issue was that ColsFromFk was simply zipping columns by position
instead of matching them correctly based on the FK relationship.

Key changes:
- Added candidateFk parameter to ColsFromFk to access FK constraint info
- Modified colPairs logic to map columns by FK relationship, not position
- Fixed column mapping to correctly match FK cols with otherCols

Before: utdanningsmulighetKode = TestUtdanningstilbudId.organisasjonskode
After:  utdanningsmulighetKode = TestUtdanningstilbudId.utdanningsmulighetKode

The fix ensures that FK columns are properly matched with their
corresponding fields in the referenced composite ID type based on
the actual foreign key constraint definition.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@oyvindberg oyvindberg force-pushed the fix/issue148-fk-column-mapping branch from 745ab12 to ef6619d Compare July 8, 2025 16:08
@oyvindberg oyvindberg merged commit f25aa4c into main Jul 8, 2025
8 checks passed
@oyvindberg oyvindberg deleted the fix/issue148-fk-column-mapping branch July 8, 2025 16: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.

Incorrect code generated

1 participant