Nestjs - QueryFailedError:uuid 类型的输入语法无效

asu*_*sus 2 postgresql uuid typeorm nestjs

我有这个方法,应该返回所有用户的所有帖子 基本上是这样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)      
    }
  }
Run Code Online (Sandbox Code Playgroud)

调用此方法时,这是 TypeORM 的响应

QueryFailedError: invalid input syntax for type uuid: "findAll"
Run Code Online (Sandbox Code Playgroud)

查询输出

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
Run Code Online (Sandbox Code Playgroud)

这是我的架构

帖子

/**
 * 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();
  }
}
Run Code Online (Sandbox Code Playgroud)

用户

/**
 * 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();
  }
}
Run Code Online (Sandbox Code Playgroud)

评论

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

为什么会发生这种情况?where为什么在没有指定的情况下却有一个子句?

TypeORM 版本:^0.2.22 TypeScript:^3.7.4

asu*_*sus 6

显然这些部分的库什是有效的,而且我不读我自己的代码。

在 中post.controller.ts@Get()装饰器缺少关键字find,我的请求如下所示:

http://localhost:3000/posts/findwherefind没有在控制器中定义为路由

解决方案是@Get('find')添加nestjs/common

后控制器.ts

  /**
   * Get all posts from all users
   */
  @Get('find')
  @ApiCreatedResponse({
    status: 201,
    description: 'All posts have been successfully retreived.',
    type: [PostDTO],
  })
  @ApiResponse({ status: 403, description: 'Forbidden.' })
  async find() {
    try {
      return this.postService.findAll();
    } catch (error) {
      throw new Error(error);
    }
  }

Run Code Online (Sandbox Code Playgroud)

后服务.ts

 /**
   * Find all posts 
   */
  async findAll(): Promise<Post[]> {
    const posts = await this.postRepository.find();
    return posts;
  }
Run Code Online (Sandbox Code Playgroud)

附言。我将添加 Nestjs 标签,因为这实际上比 TypeORM 或 PG 更相关

  • 我遇到了类似的错误,并不断回到这个问题上。最终发现(通过https://github.com/nestjs/nest/issues/1667)列出路由的顺序很重要。遗憾的是控制器文档中没有对此提出建议。在我的具体情况下,我必须对“@Get()”调用进行分组,而不是在“@Post”、“@Patch”、“@Delete”之后列出它们。 (3认同)