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
The #1 Open-Source CRM
🌐 Website · 📚 Documentation · Roadmap ·
Discord ·
Figma
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
- Customize your objects and fields
- Create and manage permissions with custom roles
- Automate workflow with triggers and actions
- Emails, calendar events, files, and more
Personalize layouts with filters, sort, group by, kanban and table views
Customize your objects and fields
Create and manage permissions with custom roles
Automate workflow with triggers and actions
Emails, calendar events, files, and more
Stack
- TypeScript
- Nx
- NestJS, with BullMQ, PostgreSQL, Redis
- React, with Jotai, Emotion and Lingui
Thanks
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
- Star the repo
- Subscribe to releases (watch -> custom -> releases)
- Follow us on Twitter or LinkedIn
- Join our Discord
- Improve translations on Crowdin
- Contributions are, of course, most welcome!




