From feb047b9101344b11b2c7c207f82e511c62c1ac9 Mon Sep 17 00:00:00 2001 From: Sonarly Claude Code Date: Tue, 14 Apr 2026 09:49:18 +0000 Subject: [PATCH] fix(application): nullify file FK references before deleting application files on uninstall MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sonarly.com/issue/25146?type=bug Uninstalling an application fails with a foreign key constraint violation because `deleteApplicationFiles` attempts to delete file records that are still referenced by the application's `packageJsonFileId`/`yarnLockFileId` columns with `onDelete: RESTRICT`. Fix: Added an `applicationRepository.update()` call to nullify `packageJsonFileId` and `yarnLockFileId` on the application row BEFORE calling `deleteApplicationFiles()`. **Why this fixes the bug:** The `application` table has two `OneToOne` FK columns (`packageJsonFileId`, `yarnLockFileId`) pointing to `file.id` with `onDelete: RESTRICT`. When `deleteApplicationFiles` tries to delete file rows matching `applicationId`, PostgreSQL blocks it because the application row still references those files via these FK columns. By setting them to `null` first, the FK constraint is satisfied and the file deletion succeeds. **Operation order after fix:** 1. `UPDATE application SET packageJsonFileId = NULL, yarnLockFileId = NULL` — breaks the FK reference 2. `DELETE FROM file WHERE applicationId = X` — now succeeds (no RESTRICT violation) 3. `DELETE FROM application WHERE universalIdentifier = X` — succeeds (files already deleted, no RESTRICT from file.applicationId) --- .../engine/core-modules/application/application.service.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/twenty-server/src/engine/core-modules/application/application.service.ts b/packages/twenty-server/src/engine/core-modules/application/application.service.ts index 6e67d8b63ec..9d738347fd8 100644 --- a/packages/twenty-server/src/engine/core-modules/application/application.service.ts +++ b/packages/twenty-server/src/engine/core-modules/application/application.service.ts @@ -452,6 +452,13 @@ export class ApplicationService { ); } + // Nullify file FK references before deleting files to avoid + // RESTRICT constraint violation (application references file via packageJsonFileId/yarnLockFileId) + await this.applicationRepository.update( + { universalIdentifier, workspaceId }, + { packageJsonFileId: null, yarnLockFileId: null }, + ); + await this.fileStorageService.deleteApplicationFiles({ workspaceId, applicationUniversalIdentifier: universalIdentifier,