Typeorm Createquerybuilder无法管理JOIN

时间:2019-03-31 22:33:38

标签: mysql node.js typescript typeorm

我是Typeorm的新手。 我正在使用节点,打字稿,TypeOrm和Mysql。 我无法弄清楚JOIN和Createquerybuilder在TypeOrm中是如何工作的。

我有一个带有user_id字段的Response表,该表与一个User表及其id字段匹配。 该关系为一对多关系中的response.user_id =user.id。 而且我不知道它如何与TypeOrm一起使用

import { Entity, Column, PrimaryColumn, PrimaryGeneratedColumn, OneToMany } from 'typeorm';
import { User } from '../User/User';

@Entity("response")
export class Response {

  @PrimaryColumn("varchar", { length: 80, unique: true, primary: true })
  public id: string;

  @PrimaryGeneratedColumn()
  public bk_id: number;

  @Column("varchar", { length: 80 })
  public user_id: string;

  @Column("varchar", { length: 80 })
  public parent_request_id: string;

  @Column("text")
  public description: string;

  @Column("integer")
  public status_id: number;  

  @Column("integer")
  public deleted: number;

  @Column("timestamp")
  public created_on: Date;

  @Column("timestamp")
  public last_update: Date;

  @OneToMany(type => User, user => user.id)
    users: User[];
}

和用户:

import { Entity, Column, PrimaryColumn, ManyToOne } from 'typeorm';
import { Response } from '../Response/Response';

@Entity("user")
export class User {

  @PrimaryColumn("varchar", { length: 50, unique: true, primary: true })
  public id: string;

  @Column("varchar", { length: 255, unique: true })
  public username: string;

  @Column("varchar", { length: 255 })
  public password: string;

  @Column("varchar", { length: 150 })
  public first_name: string;

  @Column("varchar", { length: 255 })
  public last_name: string;

  @Column("varchar", { length: 355, unique: true })
  public email: string;

  @Column("timestamp")
  public created_on: Date;

  @Column("integer")
  public userstatus_id: number;  

  @Column("integer")
  public deleted: number;

  @Column("timestamp")
  public last_login: Date;

  @ManyToOne(type => Response, response => response.user_id)
  response: Response;
}

我的Sql语句如下:

   const result = await this.entityManager
   .createQueryBuilder('Response')
   .leftJoinAndSelect(
     'Response.user_id',
     'user',
     'user.id = :id',
     { id: 'user_id' },
   )
   .where('response.parent_request_id = :parent_request_id', { parentRequestId });

我遇到了错误:

"Response" alias was not found. Maybe you forgot to join it?

感谢任何帮助。

非常感谢您。

2 个答案:

答案 0 :(得分:1)

可能是因为:

const result = await this.entityManager
.createQueryBuilder('Response') // << entity with '' and lacking alias
.leftJoinAndSelect(
  'Response.user_id',
  'user',
  'user.id = :id',
  { id: 'user_id' }, // < with '' would set as string (user_id)
)
.where('response.parent_request_id = :parent_request_id', { parentRequestId }); // < you forgot to declare param

您可以尝试以下操作:

const result = await this.entityManager
.createQueryBuilder(Response, 'response')
.leftJoinAndSelect(
  'response.user_id',
  'user',
  'user.id = :id',
  { id: user_id }, // assuming you're using user_id as variable
)
.where('response.parent_request_id = :parent_request_id', { parent_request_id: parentRequestId });

答案 1 :(得分:0)

我终于使用了Sql Query,而不是Query builder。 我认为查询生成器的构造非常棘手,因此我更愿意掌握所有内容。这是我的解决方案:

this.entityManager.query(
        `SELECT r.*, u.first_name, u.last_name, u.email, u.deleted, u.username, u.userstatus_id FROM knowhow.response as r
        LEFT JOIN user as u on u.id = r.user_id
        WHERE r.parent_request_id = '${parentRequestId}'`