From 709d9275441e940c0fd8333d80d1c0f145ff1932 Mon Sep 17 00:00:00 2001 From: Philipinho <16838612+Philipinho@users.noreply.github.com> Date: Thu, 23 Apr 2026 14:31:26 +0100 Subject: [PATCH] fix(base): declare primary key on loaded rows so upsert has a conflict target --- apps/server/src/core/base/query-cache/collection-loader.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/apps/server/src/core/base/query-cache/collection-loader.ts b/apps/server/src/core/base/query-cache/collection-loader.ts index 395925c6..c4abbe23 100644 --- a/apps/server/src/core/base/query-cache/collection-loader.ts +++ b/apps/server/src/core/base/query-cache/collection-loader.ts @@ -85,6 +85,12 @@ export class CollectionLoader { // Postgres; all subsequent queries run purely against the local table. await this.pgExtension.detach(connection); + // CREATE TABLE AS copies data but not constraints. Re-declare the primary + // key so INSERT OR REPLACE (used by applyChange.upsertRow) has a conflict + // target. This also backs id lookups with an implicit index, speeding up + // per-row upsert/delete. + await connection.run('ALTER TABLE rows ADD PRIMARY KEY (id)'); + // Build ART indexes on indexable columns. for (const spec of specs) { if (!spec.indexable) continue;