Sonarly Claude Code a8d37db19c fix: add indexes on timelineActivity FK columns for custom object morph relations
https://sonarly.com/issue/16678?type=bug

The `timelineActivity` table lacks database indexes on foreign key columns created for custom object morph relations, causing a 3,491ms sequential scan when loading a single custom object record's timeline activities.

Fix: **What changed:** When creating a custom object, Twenty automatically creates default relations to standard objects like `timelineActivity`, `noteTarget`, `taskTarget`, `attachment`, and `favorite`. The function `generateMorphOrRelationFlatFieldMetadataPair` already generates both the relation field metadata AND a BTREE index on the FK join column for the MANY_TO_ONE side. However, in `buildDefaultRelationFlatFieldMetadatasForCustomObject`, the destructuring `const { flatFieldMetadatas } = generateMorphOrRelationFlatFieldMetadataPair(...)` was silently discarding the `indexMetadatas` return value.

**The fix** (2 files, ~10 lines changed):

1. **`build-default-relation-flat-field-metadatas-for-custom-object.util.ts`**: Added `standardRelationIndexMetadatas` to the accumulator record type. Now destructures `indexMetadatas` alongside `flatFieldMetadatas` and accumulates them across all default relation objects.

2. **`from-create-object-input-to-flat-object-metadata-and-flat-field-metadatas-to-create.util.ts`**: Spreads the new `standardRelationIndexMetadatas` into `flatIndexMetadataToCreate` alongside the existing search vector GIN index.

**Why this is the right fix:** The index generation logic was already correct in `generateMorphOrRelationFlatFieldMetadataPair` (used successfully for user-created relation fields). The bug was purely that the custom object creation pipeline discarded the generated indexes. This was explicitly noted as a TODO in commit `fbd3286792` ("Index v2 side effects"): *"Will handle morph indexes in a new dedicated PR."*

**Note for existing workspaces:** This fix only affects NEW custom object creation. Existing workspaces with custom objects will still have missing indexes on their standard object tables. A one-time migration or `workspace:sync-metadata` run would be needed to backfill those indexes for existing custom objects.
2026-03-19 20:04:22 +00:00
2025-08-07 17:02:12 +02:00
2025-07-08 15:13:02 +02:00
2026-03-11 16:30:28 +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%