typeorm-polymorphic icon indicating copy to clipboard operation
typeorm-polymorphic copied to clipboard

Children not populated when retrieving parent

Open Pikachews opened this issue 1 year ago • 0 comments

Not sure if this is supposed to be a capability of this library or not, but when I add the following unit test to this library to test whether it works:

      describe('parent has children', async () => {
        it('Can find parent with children', async () => {
          const repository = AbstractPolymorphicRepository.createRepository(
            connection,
            AdvertRepository,
          );
          const userRepository = connection.getRepository(UserEntity);

          const user = await userRepository.save(new UserEntity());

          await repository.save([
            repository.create({
              owner: user,
            }),
            repository.create({
              owner: user,
            }),
          ]);

          const result = await userRepository.find();
          result.forEach((res) => {
            expect(res).toBeInstanceOf(UserEntity);
            expect(res.adverts).toBeInstanceOf(Array);
            expect(res.adverts.length).toBe(2);
          });
        });
      });

users returned from userRepository.find() always have res.adverts === undefined. I would've expected this field to be populated when retrieving from the repository. I also added eager: true to the adverts field of UserEntity but that didn't make any difference.

Am I doing something wrong?

Pikachews avatar Feb 11 '24 00:02 Pikachews