postgresql - NestJS/TypeORM 错误:插入记录时作为 UUID 传递的值不是字符串
问题描述
我创建了一个 NestJS 示例应用程序,它使用 TypeORM 来访问 Postgres 数据库。
有两个这样的实体。
@Entity({ name: 'posts' })
export class PostEntity {
@PrimaryGeneratedColumn('uuid')
id?: string;
@Column()
title: string;
@Column({ nullable: true })
content?: string;
@OneToMany((type) => CommentEntity, (comment) => comment.post, {
cascade: true,
})
comments?: Promise<CommentEntity[]>;
@ManyToOne((type) => UserEntity, { nullable: true })
@JoinColumn({ name: 'author_id' })
author?: UserEntity;
@RelationId((post: PostEntity) => post.author)
authorId?: string;
@CreateDateColumn({ name: 'created_at', type: 'timestamp', nullable: true })
createdAt?: Date;
@UpdateDateColumn({ name: 'updated_at', type: 'timestamp', nullable: true })
updatedAt?: Date;
}
@Entity({ name: 'comments' })
export class CommentEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
content: string;
@ManyToOne((type) => PostEntity, (p) => p.comments)
@JoinColumn({ name: 'post_id' })
post: PostEntity;
@RelationId((comment: CommentEntity) => comment.post)
postId?: string;
@CreateDateColumn({ name: 'created_at', type: 'timestamp' })
createdAt: Date;
}
通过 GraphQL 端点添加评论时,将调用以下代码。
addComment(id: string, comment: string): Observable<Comment> {
const entity = new CommentEntity();
Object.assign(entity, {
content: comment,
postId: id,
});
return from(this.commentRepository.save(entity)).pipe(
map((c) => {
return { id: c.id, content: c.content } as Comment;
}),
);
}
运行 e2e 测试时,它将因错误消息而失败(我添加了一个console.log
以在响应正文中打印 GraphQL 错误):
addComment errors: [{"message":"The value passed as UUID is not a string"}]
解决方案
推荐阅读
- javascript - 为什么不能在外面获取数据
- flutter - 颤振:没有名为“clipBehavior”的命名参数,clipBehavior:Clip.none
- c++ - 内存访问冲突
- html - 如何在每个列表项的末尾添加雪佛龙,从右对齐
- deep-learning - 如何在预训练的 BERT 模型之上添加多类多标签层?
- android - 我正在尝试访问外部存储目录,以获取歌曲列表并在 RecyclerView 中显示它们
- javascript - HTML5 Canvas Recording 在新的 Blob 记录时崩溃Chunks
- pug - 如何根据变量渲染块?哈巴狗(翡翠)
- java - JNA:如何在结构中指定可变长度(0+)数组?
- javascript - 在 JavaScript 自适应卡片中提交时禁用提交按钮/加载卡片