fix(conversation): add indexes and fix column types to match database

- Add @Index decorators for conversation_id, created_at, role
- Set created_at to timestamptz type to match database
- Set columns nullable to match database schema

This prevents synchronize:true from trying to modify columns
that have dependent indexes.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-24 19:41:19 -08:00
parent a821df8dc1
commit 6718fdc9e3
1 changed files with 8 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import {
CreateDateColumn,
ManyToOne,
JoinColumn,
Index,
} from 'typeorm';
import { ConversationEntity } from './conversation.entity';
@ -35,11 +36,14 @@ export const MessageType = {
export type MessageTypeType = (typeof MessageType)[keyof typeof MessageType];
@Entity('messages')
@Index('idx_messages_conversation_id', ['conversationId'])
@Index('idx_messages_created_at', ['createdAt'])
@Index('idx_messages_role', ['role'])
export class MessageEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ name: 'conversation_id', type: 'uuid' })
@Column({ name: 'conversation_id', type: 'uuid', nullable: true })
conversationId: string;
@Column({ length: 20 })
@ -56,13 +60,13 @@ export class MessageEntity {
// ========== Token统计字段与evolution-service保持一致==========
@Column({ name: 'input_tokens', default: 0 })
@Column({ name: 'input_tokens', nullable: true })
inputTokens: number;
@Column({ name: 'output_tokens', default: 0 })
@Column({ name: 'output_tokens', nullable: true })
outputTokens: number;
@CreateDateColumn({ name: 'created_at' })
@CreateDateColumn({ name: 'created_at', type: 'timestamptz', nullable: true })
createdAt: Date;
@ManyToOne(() => ConversationEntity, (conversation) => conversation.messages)