Sonarly Claude Code 4ee15a60b3 Morph relation persist uses wrong foreign key naming, producing invalid field parentObjectId
https://sonarly.com/issue/8116?type=bug

When editing a morph relation field on a task record via the Field Widget, `usePersistField.ts` constructs the wrong foreign key name (`parentObjectId` instead of e.g. `parentObjectCompanyId`), causing the optimistic cache validation to throw.

Fix: The morph relation branch in `usePersistField.ts` (lines 220–243, introduced by regression commit `4e767799c6`) used `getForeignKeyNameFromRelationFieldName(fieldName)` to construct the update key, producing `parentObjectId` instead of the required target-specific key like `parentObjectCompanyId`. This caused `computeOptimisticRecordFromInput` to throw because `parentObjectId` doesn't match any known field pattern for morph relations.

The fix replaces the broken morph relation branch with the correct approach that mirrors `useMorphPersistManyToOne`:

1. **Extracts `morphRelations` and `relationType`** from the field definition metadata (cast as `FieldMorphRelationMetadata`).
2. **Builds the null-out record** using `buildRecordWithAllMorphObjectIdsToNull` — this correctly zeroes out all morph target IDs (e.g., both `parentObjectCompanyId` and `parentObjectPersonId`) before setting the new one.
3. **For null values**: sends the all-null record to clear the relation.
4. **For non-null values**: finds the matching `morphRelation` by comparing `valueToPersist.__typename` (e.g., `"Company"`) against each `targetObjectMetadata.nameSingular` (e.g., `"company"`), then uses `computeMorphRelationFieldName()` to compute the correct key (e.g., `parentObjectCompany`), and sends `{ ...allNull, parentObjectCompanyId: valueToPersist.id }`.

```typescript file=packages/twenty-front/src/modules/object-record/record-field/ui/hooks/usePersistField.ts lines=222-292
if (fieldIsMorphRelationManyToOne) {
  if (valueToPersist?.id === currentValue?.id) {
    return;
  }

  const morphFieldDefinition =
    fieldDefinition as FieldDefinition<FieldMorphRelationMetadata>;
  const { morphRelations, relationType } =
    morphFieldDefinition.metadata;

  const recordWithAllMorphObjectIdsToNull =
    buildRecordWithAllMorphObjectIdsToNull({
      morphRelations,
      fieldName,
      relationType,
    });

  if (!valueToPersist) {
    // null out all morph IDs to clear the relation
    const newRecord = await updateOneRecord({ ... });
    upsertRecordsInStore({ ... });
    return;
  }

  const targetMorphRelation = morphRelations.find(
    (morphRelation) =>
      morphRelation.targetObjectMetadata.nameSingular.toLowerCase() ===
      valueToPersist.__typename?.toLowerCase(),
  );

  const computedFieldName = computeMorphRelationFieldName({
    fieldName,
    relationType,
    targetObjectMetadataNameSingular: targetMorphRelation.targetObjectMetadata.nameSingular,
    targetObjectMetadataNamePlural: targetMorphRelation.targetObjectMetadata.namePlural,
  });

  // Produces e.g. "parentObjectCompanyId" ✓ instead of "parentObjectId" ✗
  const newRecord = await updateOneRecord({
    updateOneRecordInput: {
      ...recordWithAllMorphObjectIdsToNull,
      [`${computedFieldName}Id`]: valueToPersist.id,
    },
  });
  ...
}
```

Two additional imports were added:
- `computeMorphRelationFieldName` from `twenty-shared/utils`
- `buildRecordWithAllMorphObjectIdsToNull` from the local utils path
2026-03-03 03:39:12 +00:00
2025-08-07 17:02:12 +02:00
2025-07-08 15:13:02 +02:00
2026-02-25 12:26:42 +01:00
2026-02-25 10:35:46 +01:00
2026-03-02 12:06:05 +01:00

Twenty logo

The #1 Open-Source CRM

🌐 Website · 📚 Documentation · Roadmap · Discord · Figma


Cover


Installation

See: 🚀 Self-hosting 🖥️ Local Setup

Why Twenty

We built Twenty for three reasons:

CRMs are too expensive, and users are trapped. Companies use locked-in customer data to hike prices. It shouldn't be that way.

A fresh start is required to build a better experience. We can learn from past mistakes and craft a cohesive experience inspired by new UX patterns from tools like Notion, Airtable or Linear.

We believe in Open-source and community. Hundreds of developers are already building Twenty together. Once we have plugin capabilities, a whole ecosystem will grow around it.


What You Can Do With Twenty

Please feel free to flag any specific needs you have by creating an issue.

Below are a few features we have implemented to date:

Personalize layouts with filters, sort, group by, kanban and table views

Companies Kanban Views

Customize your objects and fields

Setting Custom Objects

Create and manage permissions with custom roles

Permissions

Automate workflow with triggers and actions

Workflows

Emails, calendar events, files, and more

Other Features


Stack

Thanks

Chromatic Greptile Sentry Crowdin E2B

Thanks to these amazing services that we use and recommend for UI testing (Chromatic), code review (Greptile), catching bugs (Sentry) and translating (Crowdin).

Join the Community

Languages
TypeScript 78%
MDX 18.5%
JavaScript 3.1%
Python 0.2%