typeorm 0.3.7 与 typeorm 0.3.20 - 加载关系可能存在问题

问题描述 投票:0回答:1

我有以下疑问:

  async findOneByAddress(address: Address): Promise<Account | null> {
    const where: FindOptionsWhere<Account> = {
      account: {
        subacct: { 
          address: Equal(address.toString())
        },
      },
    };

    return await this.accountsRepository.findOne({ 
      where, 
      ['user', 'account', 'account.subacct'],
    });
  }

在 typeorm 0.3.7 中,它加载所有 5 个 utxoAccountAddresses。在 typeorm 0.3.20 中,它只加载匹配的。此行为对于

find
和查询生成器查询均适用。

是否有新的选项可以让我在新版本的 typeorm 中使用旧的关系构建器?

这是一个错误吗?

typeorm
1个回答
0
投票

据我所知,但这里有一个可能的解决方法,可以解决 TypeORM 0.3.20 仅加载匹配的 utxoAccountAddress 而不是所有关联的问题,您可以执行两步过程。首先,根据与给定地址匹配的 utxoAccountAddress 查找帐户。然后,手动加载与找到的帐户的 utxoAccount 关联的所有 utxoAccountAddresses。此方法需要额外的步骤,但可确保您获得与帐户相关的所有 utxoAccountAddresses。

希望这有帮助。干杯。

 async findOneByUtxoAddress(address: CryptoAddress): Promise<Account | null> {
  // Step 1: Find the Account with the matching utxoAccountAddress
  const account = await this.accountsRepository.findOne({
    where: {
      utxoAccount: {
        utxoAccountAddresses: { address: Equal(address.toString()) },
      },
    },
    relations: {
      utxoAccount: true, // Make sure to load the utxoAccount relationship
    },
  });

  if (!account) {
    return null; // Return null if no account was found
  }

  // Step 2: Explicitly load all utxoAccountAddresses for the utxoAccount of the found Account
  // Assuming `utxoAccountId` is accessible or you have a similar identifier to use
  const utxoAccountId = account.utxoAccount.id; // Adjust according to your actual data structure
  const utxoAccountAddresses = await this.utxoAccountAddressRepository.find({
    where: {
      utxoAccount: { id: utxoAccountId },
    },
  });

  // Manually set the utxoAccountAddresses to the account.utxoAccount.utxoAccountAddresses
  account.utxoAccount.utxoAccountAddresses = utxoAccountAddresses;

  return account;
}
© www.soinside.com 2019 - 2024. All rights reserved.