feat: 更新数据库架构,增加分类表及相关约束

This commit is contained in:
seahi committed 2026-07-26 17:52:39 +08:00
1 parent 94da2091e7
commit 8fb1f7da94
1 file changed
+21 -9
+21 -9
View File
@@ -1,7 +1,8 @@
DROP TABLE IF EXISTS posts;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS tags;
DROP TABLE IF EXISTS post_tags; DROP TABLE IF EXISTS post_tags;
DROP TABLE IF EXISTS posts;
DROP TABLE IF EXISTS tags;
DROP TABLE IF EXISTS categories;
DROP TABLE IF EXISTS users;
CREATE TABLE users ( CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -10,6 +11,12 @@ CREATE TABLE users (
created_at DATETIME DEFAULT CURRENT_TIMESTAMP created_at DATETIME DEFAULT CURRENT_TIMESTAMP
); );
CREATE TABLE categories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
slug TEXT NOT NULL UNIQUE
);
CREATE TABLE posts ( CREATE TABLE posts (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
slug TEXT NOT NULL UNIQUE, slug TEXT NOT NULL UNIQUE,
@@ -17,9 +24,11 @@ CREATE TABLE posts (
excerpt TEXT, excerpt TEXT,
content TEXT NOT NULL, content TEXT NOT NULL,
author_id INTEGER NOT NULL, author_id INTEGER NOT NULL,
category_id INTEGER,
published_at DATETIME DEFAULT CURRENT_TIMESTAMP, published_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author_id) REFERENCES users(id) FOREIGN KEY (author_id) REFERENCES users(id),
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE SET NULL
); );
CREATE TABLE tags ( CREATE TABLE tags (
@@ -35,18 +44,21 @@ CREATE TABLE post_tags (
FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE
); );
-- Insert a default user (password should be hashed in a real scenario, this is a placeholder) -- Insert a default user
INSERT INTO users (username, password_hash) VALUES ('admin', 'admin'); INSERT INTO users (username, password_hash) VALUES ('admin', 'admin');
-- Insert some dummy posts -- Insert default categories
INSERT INTO posts (slug, title, excerpt, content, author_id) VALUES INSERT INTO categories (name, slug) VALUES ('默认分类', 'uncategorized'), ('技术分享', 'tech'), ('生活随笔', 'life');
-- Insert dummy posts
INSERT INTO posts (slug, title, excerpt, content, author_id, category_id) VALUES
('hello-world', 'Hello, BlogFlare!', 'This is my first post on BlogFlare.', 'Welcome to my new blog powered by **SvelteKit** and **Cloudflare D1**. ('hello-world', 'Hello, BlogFlare!', 'This is my first post on BlogFlare.', 'Welcome to my new blog powered by **SvelteKit** and **Cloudflare D1**.
> [!NOTE] > [!NOTE]
> This is a DoIt style admonition! > This is a DoIt style admonition!
It is incredibly fast and cheap to host.', 1), It is incredibly fast and cheap to host.', 1, 2),
('void-theme', 'The Beauty of VOID', 'Typography is essential.', 'Typecho Theme VOID inspired this design. It uses a very clean layout.', 1); ('void-theme', 'The Beauty of VOID', 'Typography is essential.', 'Typecho Theme VOID inspired this design. It uses a very clean layout.', 1, 2);
INSERT INTO tags (name) VALUES ('Welcome'), ('Design'); INSERT INTO tags (name) VALUES ('Welcome'), ('Design');
INSERT INTO post_tags (post_id, tag_id) VALUES (1, 1), (2, 2); INSERT INTO post_tags (post_id, tag_id) VALUES (1, 1), (2, 2);