yourname 7 месяцев назад
Родитель
Сommit
f8aee0ffb5

+ 31 - 0
src/server/migrations/2025073001-update-silver-knowledge-category-relation.sql

@@ -0,0 +1,31 @@
+-- 更新银龄知识表结构,修复category关系
+-- 创建时间: 2025-07-30
+-- 描述: 将category字段从varchar改为外键关系
+
+-- 检查并更新表结构
+ALTER TABLE `silver_knowledges` 
+DROP COLUMN IF EXISTS `category`,
+ADD COLUMN IF NOT EXISTS `category_id` int unsigned DEFAULT NULL COMMENT '分类ID' AFTER `user_id`;
+
+-- 添加外键约束
+ALTER TABLE `silver_knowledges` 
+ADD CONSTRAINT IF NOT EXISTS `fk_knowledge_category` 
+FOREIGN KEY (`category_id`) REFERENCES `silver_knowledge_categories` (`id`) 
+ON DELETE SET NULL ON UPDATE CASCADE;
+
+-- 创建索引
+ALTER TABLE `silver_knowledges` 
+ADD INDEX IF NOT EXISTS `idx_category_id` (`category_id`);
+
+-- 更新现有数据的category_id字段
+-- 假设有一些默认的分类ID
+UPDATE `silver_knowledges` SET `category_id` = 1 WHERE `category_id` IS NULL;
+
+-- 验证外键约束
+SELECT 
+    k.id,
+    k.title,
+    c.name as category_name
+FROM silver_knowledges k
+LEFT JOIN silver_knowledge_categories c ON k.category_id = c.id
+LIMIT 10;

+ 11 - 5
src/server/modules/silver-users/silver-knowledge.entity.ts

@@ -1,6 +1,7 @@
 import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, ManyToOne, JoinColumn } from 'typeorm';
 import { z } from '@hono/zod-openapi';
 import { UserEntity } from '../users/user.entity';
+import { SilverKnowledgeCategory } from './silver-knowledge-category.entity';
 
 @Entity('silver_knowledges')
 export class SilverKnowledge {
@@ -13,8 +14,12 @@ export class SilverKnowledge {
   @Column({ name: 'content', type: 'text' })
   content!: string;
 
-  @Column({ name: 'category', type: 'varchar', length: 50 })
-  category!: string;
+  @Column({ name: 'category_id', type: 'int', unsigned: true })
+  categoryId!: number;
+
+  @ManyToOne(() => SilverKnowledgeCategory)
+  @JoinColumn({ name: 'category_id' })
+  category!: SilverKnowledgeCategory;
 
   @Column({ name: 'tags', type: 'text', nullable: true })
   tags!: string | null;
@@ -50,7 +55,8 @@ export const SilverKnowledgeSchema = z.object({
   id: z.number().int().positive().openapi({ description: '知识ID' }),
   title: z.string().max(255).openapi({ description: '知识标题', example: '健康生活小贴士' }),
   content: z.string().openapi({ description: '知识内容', example: '今日分享几个健康生活的小技巧...' }),
-  category: z.string().max(50).openapi({ description: '知识分类', example: '健康养生' }),
+  categoryId: z.number().int().positive().openapi({ description: '分类ID', example: 1 }),
+  category: z.any().optional().openapi({ description: '知识分类信息' }),
   tags: z.string().nullable().openapi({ description: '标签,逗号分隔', example: '健康,养生,老年人' }),
   author: z.string().max(100).openapi({ description: '作者', example: '张医生' }),
   status: z.number().int().min(0).max(1).openapi({ description: '状态(0-草稿,1-发布)', example: 1 }),
@@ -64,7 +70,7 @@ export const SilverKnowledgeSchema = z.object({
 export const CreateSilverKnowledgeDto = z.object({
   title: z.string().max(255).openapi({ description: '知识标题', example: '健康生活小贴士' }),
   content: z.string().openapi({ description: '知识内容', example: '今日分享几个健康生活的小技巧...' }),
-  category: z.string().max(50).openapi({ description: '知识分类', example: '健康养生' }),
+  categoryId: z.coerce.number().int().positive().openapi({ description: '分类ID', example: 1 }),
   tags: z.string().nullable().optional().openapi({ description: '标签,逗号分隔', example: '健康,养生,老年人' }),
   author: z.string().max(100).openapi({ description: '作者', example: '张医生' }),
   status: z.coerce.number().int().min(0).max(1).default(1).openapi({ description: '状态(0-草稿,1-发布)', example: 1 })
@@ -73,7 +79,7 @@ export const CreateSilverKnowledgeDto = z.object({
 export const UpdateSilverKnowledgeDto = z.object({
   title: z.string().max(255).optional().openapi({ description: '知识标题', example: '健康生活小贴士' }),
   content: z.string().optional().openapi({ description: '知识内容', example: '今日分享几个健康生活的小技巧...' }),
-  category: z.string().max(50).optional().openapi({ description: '知识分类', example: '健康养生' }),
+  categoryId: z.coerce.number().int().positive().optional().openapi({ description: '分类ID', example: 1 }),
   tags: z.string().nullable().optional().openapi({ description: '标签,逗号分隔', example: '健康,养生,老年人' }),
   author: z.string().max(100).optional().openapi({ description: '作者', example: '张医生' }),
   status: z.coerce.number().int().min(0).max(1).optional().openapi({ description: '状态(0-草稿,1-发布)', example: 1 })