Skip to content

Commit 47a3f18

Browse files
yyyCodeclaude
andcommitted
fix(sql): remove foreign key constraints from forum tables
FK references to users table fail when users table doesn't exist yet. Application layer ensures data consistency. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 3d10789 commit 47a3f18

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

sql/forum.sql

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
-- 论坛话题表
2+
-- 注意:未加外键约束,因为目标库可能尚无 users 表,应用层保证数据一致性
23
CREATE TABLE IF NOT EXISTS forum_topics (
34
id BIGINT AUTO_INCREMENT PRIMARY KEY,
45
title VARCHAR(200) NOT NULL,
@@ -10,8 +11,7 @@ CREATE TABLE IF NOT EXISTS forum_topics (
1011
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
1112
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
1213
INDEX idx_topics_status_created (status, created_at DESC),
13-
INDEX idx_topics_author (author_id),
14-
CONSTRAINT fk_forum_topics_author FOREIGN KEY (author_id) REFERENCES users(id)
14+
INDEX idx_topics_author (author_id)
1515
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
1616

1717
-- 论坛评论表
@@ -23,7 +23,5 @@ CREATE TABLE IF NOT EXISTS forum_comments (
2323
status VARCHAR(16) NOT NULL DEFAULT 'APPROVED' COMMENT 'APPROVED / DELETED',
2424
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
2525
INDEX idx_comments_topic_created (topic_id, created_at ASC),
26-
INDEX idx_comments_author (author_id),
27-
CONSTRAINT fk_forum_comments_topic FOREIGN KEY (topic_id) REFERENCES forum_topics(id),
28-
CONSTRAINT fk_forum_comments_author FOREIGN KEY (author_id) REFERENCES users(id)
26+
INDEX idx_comments_author (author_id)
2927
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

0 commit comments

Comments
 (0)