MemberJunction / MemberJunction/MJ

CodeGen truncates EntityFieldValue.Code when auto-detecting value lists from data

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

Description

## Summary

When CodeGen auto-detects value lists for enum-like columns (e.g., membership type codes), it inserts the full display string into both the `Value` and `Code` columns of `__mj.EntityFieldValue`. The `Code` column is `nvarchar(50)`, so values exceeding 50 characters cause a truncation error:

```
RequestError: String or binary data would be truncated in table 'MemberJunction.__mj.EntityFieldValue', column 'Code'.
Truncated value: 'AFR_SCH - Primary and Secondary School Institution'.
```

## Root Cause

The `Code` column is designed as a "programmatic identifier" (per its Zod schema description: "Optional code identifier for this value, useful for programmatic access. If not specified, same as Value"). When CodeGen auto-populates value lists from distinct column values, it uses the full string for both `Code` and `Value` — but `Code` is only 50 chars wide while `Value` is 255 chars.

## Real-World Data

The `remembers.OrganizationMemberships.membership_type` column has 20 distinct values exceeding 50 characters (up to 62 chars):

| Value | Length |
|-------|--------|
| CASE Europe Non Profit Organization Educational Associate Dues | 62 |
| CASE Europe - Primary and Secondary School Institutional Dues | 61 |
| AUS_EA - Non Profit Organization Educational Associate Dues | 59 |
| Canadian Non Profit Organization Educational Associate Dues | 59 |
| ... (16 more) | 50-58 |

## Impact

- CodeGen emits `RequestError` warnings but continues execution
- The affected EntityFieldValue records are not inserted
- Downstream consumers (like AI agents) that rely on `possibleValues` may have incomplete value lists
- The error is non-fatal but noisy and causes data gaps

## Suggested Fix

When auto-detecting value lists, CodeGen should handle the Code column differently:
1. If the value exceeds 50 chars, truncate Code to 50 chars (or leave it NULL, since it's nullable)
2. Alternatively, widen the `Code` column to match `Value` at `nvarchar(255)` — the 50-char limit seems artificially restrictive for real-world data

## Environment

- MJ CLI: 5.33.0
- Database: Azure SQL (production)
- Table: `remembers.OrganizationMemberships.membership_type`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.