From 3a8b8c9cf8a89b659095233115d4c3ea34e46f45 Mon Sep 17 00:00:00 2001 From: ZhenYi <434836402@qq.com> Date: Wed, 15 Apr 2026 11:03:17 +0800 Subject: [PATCH] remove all foreign key constraints from SQL migrations Foreign keys at the database level cause issues with deployment flexibility. Keep only indexes for query performance; enforce referential integrity at the application level. --- .../m20250628_000080_add_message_reactions_and_search.sql | 6 +++--- .../sql/m20250628_000081_add_message_edit_history.sql | 4 ++-- libs/migrate/sql/m20260407_000002_create_project_board.sql | 4 ++-- .../sql/m20260411_000003_add_workspace_id_to_project.sql | 2 +- .../sql/m20260412_000001_create_workspace_billing.sql | 2 +- .../m20260412_000002_create_workspace_billing_history.sql | 2 +- libs/migrate/sql/m20260414_000001_create_agent_task.sql | 4 +--- 7 files changed, 11 insertions(+), 13 deletions(-) diff --git a/libs/migrate/sql/m20250628_000080_add_message_reactions_and_search.sql b/libs/migrate/sql/m20250628_000080_add_message_reactions_and_search.sql index 8a69341..69e558d 100644 --- a/libs/migrate/sql/m20250628_000080_add_message_reactions_and_search.sql +++ b/libs/migrate/sql/m20250628_000080_add_message_reactions_and_search.sql @@ -2,9 +2,9 @@ CREATE TABLE IF NOT EXISTS room_message_reaction ( id UUID PRIMARY KEY, - room UUID NOT NULL REFERENCES room (id) ON DELETE CASCADE, - message UUID NOT NULL REFERENCES room_message (id) ON DELETE CASCADE, - "user" UUID NOT NULL REFERENCES "user" (uid) ON DELETE CASCADE, + room UUID NOT NULL, + message UUID NOT NULL, + "user" UUID NOT NULL, emoji VARCHAR(50) NOT NULL, created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), UNIQUE (message, "user", emoji) diff --git a/libs/migrate/sql/m20250628_000081_add_message_edit_history.sql b/libs/migrate/sql/m20250628_000081_add_message_edit_history.sql index 5462334..406f6ef 100644 --- a/libs/migrate/sql/m20250628_000081_add_message_edit_history.sql +++ b/libs/migrate/sql/m20250628_000081_add_message_edit_history.sql @@ -1,8 +1,8 @@ CREATE TABLE IF NOT EXISTS room_message_edit_history ( id UUID PRIMARY KEY, - message UUID NOT NULL REFERENCES room_message (id) ON DELETE CASCADE, - "user" UUID NOT NULL REFERENCES "user" (uid) ON DELETE CASCADE, + message UUID NOT NULL, + "user" UUID NOT NULL, old_content TEXT NOT NULL, new_content TEXT NOT NULL, edited_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() diff --git a/libs/migrate/sql/m20260407_000002_create_project_board.sql b/libs/migrate/sql/m20260407_000002_create_project_board.sql index 9c209b1..8031419 100644 --- a/libs/migrate/sql/m20260407_000002_create_project_board.sql +++ b/libs/migrate/sql/m20260407_000002_create_project_board.sql @@ -14,7 +14,7 @@ CREATE INDEX IF NOT EXISTS idx_project_board_project ON project_board (project_u CREATE TABLE IF NOT EXISTS project_board_column ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), - board_uuid UUID NOT NULL REFERENCES project_board (id) ON DELETE CASCADE, + board_uuid UUID NOT NULL, name VARCHAR(255) NOT NULL, position INTEGER NOT NULL DEFAULT 0, wip_limit INTEGER, @@ -26,7 +26,7 @@ CREATE INDEX IF NOT EXISTS idx_project_board_column_board ON project_board_colum CREATE TABLE IF NOT EXISTS project_board_card ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), - column_uuid UUID NOT NULL REFERENCES project_board_column (id) ON DELETE CASCADE, + column_uuid UUID NOT NULL, issue_id BIGINT, project UUID, title VARCHAR(500) NOT NULL, diff --git a/libs/migrate/sql/m20260411_000003_add_workspace_id_to_project.sql b/libs/migrate/sql/m20260411_000003_add_workspace_id_to_project.sql index 7881544..34cb64a 100644 --- a/libs/migrate/sql/m20260411_000003_add_workspace_id_to_project.sql +++ b/libs/migrate/sql/m20260411_000003_add_workspace_id_to_project.sql @@ -1,4 +1,4 @@ ALTER TABLE project - ADD COLUMN workspace_id UUID REFERENCES workspace(id) ON DELETE SET NULL; + ADD COLUMN workspace_id UUID; CREATE INDEX idx_project_workspace_id ON project (workspace_id) WHERE workspace_id IS NOT NULL; diff --git a/libs/migrate/sql/m20260412_000001_create_workspace_billing.sql b/libs/migrate/sql/m20260412_000001_create_workspace_billing.sql index 40e757a..e7a7406 100644 --- a/libs/migrate/sql/m20260412_000001_create_workspace_billing.sql +++ b/libs/migrate/sql/m20260412_000001_create_workspace_billing.sql @@ -1,5 +1,5 @@ CREATE TABLE IF NOT EXISTS workspace_billing ( - workspace_id UUID PRIMARY KEY REFERENCES workspace(id) ON DELETE CASCADE, + workspace_id UUID PRIMARY KEY, balance DECIMAL(20, 4) NOT NULL DEFAULT 0, currency VARCHAR(10) NOT NULL DEFAULT 'USD', monthly_quota DECIMAL(20, 4) NOT NULL DEFAULT 0, diff --git a/libs/migrate/sql/m20260412_000002_create_workspace_billing_history.sql b/libs/migrate/sql/m20260412_000002_create_workspace_billing_history.sql index e15b62b..19d7c23 100644 --- a/libs/migrate/sql/m20260412_000002_create_workspace_billing_history.sql +++ b/libs/migrate/sql/m20260412_000002_create_workspace_billing_history.sql @@ -1,7 +1,7 @@ CREATE TABLE IF NOT EXISTS workspace_billing_history ( uid UUID PRIMARY KEY, - workspace_id UUID NOT NULL REFERENCES workspace (id) ON DELETE CASCADE, + workspace_id UUID NOT NULL, user_id UUID, amount DECIMAL(20, 4) NOT NULL, currency VARCHAR(10) NOT NULL DEFAULT 'USD', diff --git a/libs/migrate/sql/m20260414_000001_create_agent_task.sql b/libs/migrate/sql/m20260414_000001_create_agent_task.sql index 0c8acbf..d792d26 100644 --- a/libs/migrate/sql/m20260414_000001_create_agent_task.sql +++ b/libs/migrate/sql/m20260414_000001_create_agent_task.sql @@ -22,9 +22,7 @@ CREATE TABLE IF NOT EXISTS agent_task started_at TIMESTAMPTZ, done_at TIMESTAMPTZ, -- Progress info (step count, current status text) - progress VARCHAR(255), - -- Parent task FK - CONSTRAINT fk_agent_task_parent FOREIGN KEY (parent_id) REFERENCES agent_task(id) ON DELETE SET NULL + progress VARCHAR(255) ); CREATE INDEX IF NOT EXISTS idx_agent_task_project ON agent_task (project_uuid);