【问题标题】:Typeorm find query doesn't return ManyToOne relation idTypeorm查找查询不返回ManyToOne关系ID
【发布时间】:2021-06-30 13:00:10
【问题描述】:

正如标题所说:

我有一个business-user 表和一个business-hours 表。 buiness-hoursbusiness-user 具有多对一关系。

business-hours侧的关系定义如下。

@ManyToOne(() => BusinessUser, (businessUser) => businessUser.businessHours)
@JoinColumn()
businessUser: BusinessUser;

Typeorm 文档说这应该在它所做的表中创建一个字段为businessUserId。另外,由于这是主键上的关系,所以我不需要命名或引用它。 但是当我做findfindOne 查询时,businessUserId 不再返回。当我检查数据库时,字段和值按预期存在,只是它们没有在查找查询中返回。早些时候它工作正常,我正在获取所有表的关系表 ID,但现在它不再工作了。想知道是否有人可以帮助我或弄清楚发生了什么变化。我已经尝试过这里提出的类似问题的解决方案,但没有任何帮助。

【问题讨论】:

    标签: typescript postgresql nestjs typeorm


    【解决方案1】:

    你可以尝试设置这个loadRelationIds:true

    例如我使用这样的东西:

    async findAByConditionWithRelationsOfUser(condition, relations, speaker_id) {  
         try {
              let data: SentenceEntity[] = await this.sentenceRespository.find({ where: condition, relations: relations, loadRelationIds:true });
              data = data.map(sentence => {
                for (let i = 0; i < sentence.sentenceToSpeaker.length; i++) {
                  const element = sentence.sentenceToSpeaker[i];
                  if (element.id == speaker_id) {
                    sentence['recordedAudio'] = element
                  };
                }
                delete sentence.sentenceToSpeaker;
                return sentence;
              })
              return data;
            } catch (error) {
              throw new HttpException(error, error.status)
            }
          }
    

    【讨论】:

      【解决方案2】:

      official documentation 上有一个“官方”解决方法。它解释了“如何在不加入关系的情况下使用关系 ID”。

      总而言之,您只需向您的实体添加一个myRelationId(在您的情况下,像这样(例如)向您的实体添加一个businessUserId) 列:

      @Entity()
      export class User {
      
          @PrimaryGeneratedColumn()
          id: number;
      
          @Column()
          name: string;
      
          @Column({ nullable: true })
          profileId: number;  // <---------- You'll get only the id from here
      
          @OneToOne(type => Profile)
          @JoinColumn()
          profile: Profile;  // <----------- You don't want all the data
      
      }
      

      如果您这样做,显然,请从您的请求中删除 {relations : ["businessUser"]}

      【讨论】:

        【解决方案3】:

        在您保存在多对多关系的一侧使用级联以保存到两个表中之前,当导入用户关系选项并且 businessUser 是一个 BusinessUser 数组时

        @ManyToOne(() => BusinessUser, (businessUser) => businessUser.businessHours ,{ cascade : true})
        @JoinColumn()
        businessUser: BusinessUser[];
        
        const foundOne = await this.entity.findOne(id , {relations : ["businessUser"]})
        const foundAll = await this.entity.findOne({relations : ["businessUser"]})
        

        如果你想自动加载它,请使用 eager

        @ManyToOne(() => BusinessUser, (businessUser) => businessUser.businessHours ,{ cascade : true , eager : true})
        @JoinColumn()
        businessUser: BusinessUser[];
        

        【讨论】:

        • 感谢 Kamal,但我已经知道这两个选项,但这不是我想要的。这将返回整个相关对象。我正在寻找的是 id,它已经作为外键保存在表中。它只是没有与对象的其余部分一起获取。
        猜你喜欢
        • 2021-08-09
        • 1970-01-01
        • 2021-08-10
        • 2012-11-15
        • 2020-09-02
        • 1970-01-01
        • 2020-10-18
        • 2021-01-23
        • 2020-04-29
        相关资源
        最近更新 更多