Marie c8f7315c67 fix(server): converge app-sync isUnique diff for single-column unique constraints (#22592)
## Context

Fixes #22550. On app sync (`twenty dev`), a field backed by a
single-column unique constraint produces a **permanent, non-converging
`[isUnique] changed` field-metadata diff**: the change is reported, the
apply succeeds, and the identical change is reported again on the very
next sync.

## Root cause

Since the uniqueness-source-of-truth moved from a
`FieldMetadata.isUnique` column to `IndexMetadata` (@FelixMalfait's
#20846 / #20883), `field.isUnique` is a **derived** property. The two
sides of the app-sync diff derive it differently:

- **"from" side** (workspace cache,
`WorkspaceFlatFieldMetadataMapCacheService`) derives `isUnique` from
indexes via `computeUniqueFieldMetadataIdsFromIndexes`, which counted
**any** single-column `UNIQUE` index.
- **"to" side**
(`from-field-manifest-to-universal-flat-field-metadata.util.ts`) sets
`isUnique` from the field-level manifest flag (`fieldManifest.isUnique
?? false`).

For a field whose uniqueness is declared with `defineIndex({ isUnique:
true, fields: [oneField] })` (no field-level flag):
- "from" derives `true` (the custom unique index exists),
- "to" is `false` (no field-level flag),

so the diff emits a `fieldMetadata … [isUnique] changed` update forever.
The field-update runner drops `isUnique` before the SQL `UPDATE` (it has
no column), and the custom index persists, so the derived value never
changes — the loop cannot converge.

## Fix

Restrict the derivation in `computeUniqueFieldMetadataIdsFromIndexes` to
the field's **engine-owned backing constraint** — a `UNIQUE` index with
`isSystemSideEffect: true` — rather than any single-column unique index.

This makes `field.isUnique` mean the same thing on both sides:

| declaration | backing index (`isSystemSideEffect`) | "from" derived |
"to" flag | converges |
|---|---|---|---|---|
| field-level `isUnique: true` | side-effect handler generates it →
`true` | `true` | `true` |  |
| `defineIndex({ isUnique: true, fields:[x] })` | custom index → `false`
| `false` | `false` |  (index converges on its own) |

Standard objects and the create/update side-effect backing indexes are
all `isSystemSideEffect: true`
(`create-standard-index-flat-metadata.util.ts`,
`generate-deterministic-index-for-flat-field-metadata-or-throw.util.ts`),
so their fields keep `isUnique = true`. Only a user-declared custom
`defineIndex` unique index (`isSystemSideEffect: false`) is now excluded
— which is also what stops the create/update side-effect from generating
a **second, duplicate** backing index for a field the custom index
already covers (which would otherwise trip `DUPLICATE_UNIQUE_INDEX` on
first apply).

## Why this location (and not the manifest "to" side)

An earlier attempt derived `isUnique` on the manifest "to" side from the
built indexes. That breaks after #22295: `field.isUnique === true` is
the **trigger** for the `fieldUniqueBackingIndexOnCreate/Update`
side-effect handlers, so forcing it to derive from the compute-service
maps (which don't yet contain the not-yet-generated backing index) would
suppress the backing index for field-level unique fields. Narrowing the
shared "from" derivation keeps the side-effect trigger intact and makes
both sides symmetric in one place.

## For review — @FelixMalfait

This touches the uniqueness model you own in #20883 ("make IndexMetadata
the source of truth for uniqueness"), and interacts with the side-effect
engine from #22295. The semantic change is: **`field.isUnique` now
reflects only the field's backing constraint, not an arbitrary
user-declared single-column unique index.** A `defineIndex`-declared
single-column unique field now surfaces `isUnique: false` on the field
(the constraint is still enforced by the index). If instead you'd want
`defineIndex` single-column uniqueness to surface as `field.isUnique:
true`, the fix would need to live in the side-effect engine (dedupe the
backing index against the declared one) rather than the derivation —
happy to take it that direction. Flagging for your call before this
leaves draft.

## Related

- Issue #22550
- @FelixMalfait #20883, #20846 (IndexMetadata as source of truth for
uniqueness)
- #22295 (centralized side-effect engine — unique field backing index)
- #21383 (adjacent field-`isUnique` handling)

https://claude.ai/code/session_01T1Cqvt5tHS6tZ1FeQQWyRo
2026-07-07 09:34:47 +02:00
2026-06-11 11:02:28 +02:00

Twenty logo

The #1 Open-Source CRM

Website · Documentation · Roadmap · Discord · Figma

Twenty banner


Why Twenty

Twenty gives technical teams the building blocks for a custom CRM that meets complex business needs and quickly adapts as the business evolves. Twenty is the CRM you build, ship, and version like the rest of your stack.

Learn more about why we built Twenty


Installation

Cloud

The fastest way to get started. Sign up at twenty.com and spin up a workspace in under a minute, with no infrastructure to manage and always up to date.

Build an app

Scaffold a new app with the Twenty CLI:

npx create-twenty-app my-app

Define objects, fields, and views as code:

import { defineObject, FieldType } from 'twenty-sdk/define';

export default defineObject({
  nameSingular: 'deal',
  namePlural: 'deals',
  labelSingular: 'Deal',
  labelPlural: 'Deals',
  fields: [
    { name: 'name', label: 'Name', type: FieldType.TEXT },
    { name: 'amount', label: 'Amount', type: FieldType.CURRENCY },
    { name: 'closeDate', label: 'Close Date', type: FieldType.DATE_TIME },
  ],
});

Then ship it to your workspace:

npx twenty app:publish --private

See the app development guide for objects, views, agents, and logic functions.

Self-hosting

Run Twenty on your own infrastructure with Docker Compose, or contribute locally via the local setup guide.



Everything you need

Twenty gives you the building blocks of a modern CRM (objects, views, workflows, and agents) and lets you extend them as code. Here's a tour of what's in the box.

Want to go deeper? Read the User Guide for product walkthroughs, or the Documentation for developer reference.

Create your apps

Learn more about apps in doc

Stay on top with version control

Learn more about version control in doc

All the tools you need to build anything

Learn more about primitives in doc

Customize your layouts

Learn more about layouts in doc

AI agents and chats

Learn more about AI in doc

Plus all the tools of a good CRM

Learn more about CRM features in doc


Stack

Thanks

Greptile      Sentry      Crowdin

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

Join the Community

Star the repo · Discord · Feature requests · Releases · X · LinkedIn · Crowdin · Contribute

S
Description
The open alternative to Salesforce, designed for AI.
Readme AGPL-3.0 1.4 GiB
Languages
TypeScript 79.6%
MDX 17.3%
JavaScript 2.7%
Python 0.2%
SCSS 0.1%