Skip to content

Commit 6ef313a

Browse files
waleedlatif1claude
andcommitted
fix(db): change document.connectorId FK from cascade to set null
The cascade behavior meant deleting a connector would always delete its documents, contradicting the "keep documents" option. With set null, the database automatically nullifies connectorId when a connector is removed, and we only need explicit deletion when the user opts in. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d54cf4b commit 6ef313a

File tree

3 files changed

+4
-12
lines changed

3 files changed

+4
-12
lines changed

apps/sim/app/api/knowledge/[id]/connectors/[connectorId]/route.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -314,17 +314,6 @@ export async function DELETE(request: NextRequest, { params }: RouteParams) {
314314
await tx.delete(embedding).where(inArray(embedding.documentId, documentIds))
315315
await tx.delete(document).where(inArray(document.id, documentIds))
316316
}
317-
} else if (docs.length > 0) {
318-
await tx
319-
.update(document)
320-
.set({ connectorId: null })
321-
.where(
322-
and(
323-
eq(document.connectorId, connectorId),
324-
isNull(document.archivedAt),
325-
isNull(document.deletedAt)
326-
)
327-
)
328317
}
329318

330319
const deletedConnectors = await tx
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE "document" DROP CONSTRAINT "document_connector_id_knowledge_connector_id_fk";
2+
--> statement-breakpoint
3+
ALTER TABLE "document" ADD CONSTRAINT "document_connector_id_knowledge_connector_id_fk" FOREIGN KEY ("connector_id") REFERENCES "public"."knowledge_connector"("id") ON DELETE set null ON UPDATE no action;

packages/db/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ export const document = pgTable(
12751275

12761276
// Connector-sourced document fields
12771277
connectorId: text('connector_id').references(() => knowledgeConnector.id, {
1278-
onDelete: 'cascade',
1278+
onDelete: 'set null',
12791279
}),
12801280
externalId: text('external_id'),
12811281
contentHash: text('content_hash'),

0 commit comments

Comments
 (0)