【问题标题】:Typeorm find with sub relations is not working带有子关系的Typeorm查找不起作用
【发布时间】:2020-12-08 10:56:30
【问题描述】:

使用子关系搜索时,我无法找到行。我尝试了有无承诺,但仍然没有运气。

示例实体:

@Entity('email')
export class Email extends BaseEntity {
    @Column({ nullable: false, unique: true })
    email: string;

    @Column({ default: false })
    verified: boolean;

    @ManyToOne(type => User, user => user.emails)
    user: Promise<User>;
}

@Entity('users')
export class User extends BaseEntity {
    @Column({ nullable: false })
    name: string;

    @OneToMany(type => Phone, phone => phone.user)
    emails: Promise<Email[]>;
}

如果我使用以下内容:

// user will remain null
const user = await this.userRepo.findOne({
  where: [
    {
      emails: [
        {
           email: 'test@test.com'
        }
      ],
  ],
}
// email will also remain null
const email = await this.emailRepo.findOne({
  where: [
    {
      user: {
         name: 'test'
      },
  ],
}

有谁知道如何解决这个问题?我认为这是一个错误。

【问题讨论】:

    标签: typeorm typeorm-datamapper node.js-typeorm


    【解决方案1】:

    find/findOne 不允许按嵌套关系属性进行过滤。去QueryBuilder,而不是像

    const x = await repo.createQueryBuilder("email")
        .innerJoinAndSelect("email.users", "user")
        .where("user.name = :name", { name })
        .getOne()
    

    查看here 是否有类似问题。

    【讨论】:

      猜你喜欢
      • 2021-01-01
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 2021-01-20
      相关资源
      最近更新 更多