Skip to content

feat: Prisma @relation field support with FK generation (Issue #10) - #25

Merged
Phathdt merged 4 commits into
mainfrom
feat/issue-10-prisma-relation-support
Sep 22, 2026
Merged

Phathdt merged 4 commits into
mainfrom
feat/issue-10-prisma-relation-support

Conversation

@Phathdt

@Phathdt Phathdt commented Sep 22, 2026

Copy link
Copy Markdown
Owner

Summary

Implements full support for Prisma @relation fields, enabling FK constraint generation in migrations.

Changes

Core Implementation

  • Full @relation parsing: fields, references, onDelete, onUpdate
  • FK generation: Create schema.ForeignKey from @relation attributes
  • Composite FK support: Multi-column foreign keys
  • Field/model mapping: Prisma names → DB names (respect @Map, @@Map)
  • Three-pass conversion:
    1. Collect metadata (enums, model→table, field→column maps)
    2. Convert models to internal schema
    3. Validate FK references exist in target tables

Differ Enhancement

  • Add CreateForeignKey operations for initial migrations

Test Coverage (+11 tests)

Integration Tests (7)

  • Simple relation → FK generation
  • Composite FK (multi-column)
  • Referential actions (Cascade, Restrict, SetNull, SetDefault, NoAction)
  • Nullable relations
  • Back-reference handling
  • Error cases

E2E Pipeline Tests (3)

  • Prisma → Parse → Diff → SQL with FK DDL verification
  • Composite FK SQL generation
  • Multiple independent FKs

Manual Test (1)

  • Real-world schema verification

Example Output

ALTER TABLE posts
ADD CONSTRAINT fk_posts_author_id
FOREIGN KEY (author_id)
REFERENCES users (id) ON DELETE CASCADE;

Verification

✅ All 68 integration tests pass
✅ FK names use actual table names
✅ RefColumns correctly mapped
✅ Handles forward references

Closes #10

…sue #10)

- Add full @relation parsing: fields, references, onDelete, onUpdate
- Generate schema.ForeignKey constraints from @relation attributes
- Handle composite FKs (multi-column foreign keys)
- Map Prisma field names to DB column names (respect @Map)
- Map Prisma model names to DB table names (respect @@Map)
- Validate FK reference columns exist in target tables
- Add CreateForeignKey operations in differ for initial migrations

Test coverage:
- 7 integration tests in converter_relation_integration_test.go
- 3 E2E pipeline tests in converter_e2e_test.go (Prisma→SQL)
- 1 manual test for real-world schema verification
- All 11 tests pass with full FK generation and validation

Fixes:
- FK names use actual table names (fk_posts_author_id not fk_Post_author_id)
- RefColumns correctly mapped from Prisma field names to DB column names
- Three-pass conversion: (1) collect metadata, (2) convert models, (3) validate FKs
- Build modelFieldToDbCol map in first pass to handle forward references

Closes #10
- Remove Issue #10 from Known Issues section (now implemented)
- Update test count: 57 → 68 integration tests
- Add Relations test coverage line
- Reorder v0.2 roadmap priorities
- Update implementation status date to 2026-09-21
@Phathdt
Phathdt force-pushed the feat/issue-10-prisma-relation-support branch from b7329ad to ccdbbb0 Compare September 22, 2026 02:59
@Phathdt
Phathdt merged commit 700472b into main Sep 22, 2026
7 checks passed
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.

Support Prisma relation fields in schema parser and migration generator

1 participant