【发布时间】:2020-03-10 14:57:42
【问题描述】:
我有这个方法应该返回所有用户的所有帖子
基本上是这样Select * from posts
/**
* Find all posts
*/
async findAll(): Promise<Post[]> {
try {
return await await getConnection()
.createQueryBuilder()
.select("posts")
.from(Post, "posts").getMany();
} catch (error) {
throw new Error(error)
}
}
当这个方法被调用时,这是来自 TypeORM 的响应
QueryFailedError: invalid input syntax for type uuid: "findAll"
查询输出
SELECT DISTINCT
"distinctAlias"."Post_post_id" as "ids_Post_post_id"
FROM
(
SELECT
"Post"."post_id" AS "Post_post_id",
"Post"."uid" AS "Post_uid",
"Post"."title" AS "Post_title",
"Post"."sub_title" AS "Post_sub_title",
"Post"."content" AS "Post_content",
"Post"."userUserId" AS "Post_userUserId",
"Post__comments"."comment_id" AS "Post__comments_comment_id",
"Post__comments"."content" AS "Post__comments_content",
"Post__comments"."postPostId" AS "Post__comments_postPostId",
"Post__comments"."userUserId" AS "Post__comments_userUserId"
FROM
"posts" "Post"
LEFT JOIN
"comments" "Post__comments"
ON "Post__comments"."postPostId" = "Post"."post_id"
WHERE
"Post"."post_id" = $1
)
"distinctAlias"
ORDER BY
"Post_post_id" ASC LIMIT 1
这是我的架构
帖子
/**
* Post Entity
*/
@Entity('posts')
export class Post {
@PrimaryGeneratedColumn('uuid') post_id: string;
@Column({ type: 'varchar', nullable: false, unique: true }) uid: string;
@Column('text') title: string;
@Column('text') sub_title: string;
@Column('text') content: string;
@ManyToOne(
type => User,
user => user.posts,
{
cascade: true,
},
)
user: User;
@OneToMany(
type => Comment,
comment => comment.post,
{
cascade: true,
},
)
comments: Comment[];
constructor(title?: string, content?: string) {
this.title = title || '';
this.content = content || '';
}
@BeforeInsert() async generateUID() {
this.uid = uuid();
}
}
用户
/**
* User Entity
*/
@Entity('users')
export class User {
@PrimaryGeneratedColumn('uuid') user_id: string;
@Column({ type: 'varchar', nullable: false, unique: true }) uid: string;
@Column({ type: 'varchar', nullable: false }) name: string;
@Column({ type: 'varchar', nullable: false, unique: true }) email: string;
@Column({ type: 'varchar', nullable: false, unique: true }) username: string;
@Column({ type: 'varchar', nullable: false }) password: string;
@OneToMany(
type => Post,
post => post.user,
{
eager: true,
},
)
posts: Post[];
@OneToMany(
type => Comment,
comment => comment.user,
)
comments: Comment[];
constructor(name?: string, posts?: []);
constructor(name?: string) {
this.name = name || '';
}
@BeforeInsert() async hashPassword() {
this.password = await bcrypt.hash(this.password, 10);
this.uid = uuid();
}
}
评论
/**
* Comments Entity
*/
@Entity('comments')
export class Comment {
@PrimaryGeneratedColumn('uuid') comment_id: string;
@Column('text') content: string;
@ManyToOne(
type => Post,
post => post.comments,
)
post: Post;
@ManyToOne(
type => User,
user => user.comments,
)
user: User;
}
为什么会这样?
为什么没有指定where 子句?
TypeORM 版本:^0.2.22 打字稿:^3.7.4
【问题讨论】:
标签: postgresql uuid nestjs typeorm