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

EntityMetadataNotFoundError: No metadata for "null" was found

Open eliasjtg opened this issue 1 month ago • 0 comments

How to implement nullable relationship?

I'm implementing this package with NestJS, all work fine except quering when relationship is null.

Entity schema:

@Entity({
  name: 'businesses',
})
export class Business extends Base implements BusinessInterface {
  @ApiProperty({
    description: 'The business name',
    type: 'string',
    example: 'Plass',
  })
  @Column({
    nullable: false,
  })
  name!: string;

  @ApiProperty({
    description: 'Business active',
    type: 'boolean',
    example: true,
  })
  @Column({
    nullable: false,
    default: true,
  })
  active!: boolean;

  @Column({
    nullable: true,
    enum: IntegrableEnum,
  })
  integrableType?: string;

  @Column({
    nullable: true,
  })
  integrableId?: string;

  @PolymorphicParent(() => [SiigoIntegration], {
    entityTypeColumn: 'integrableType',
    entityTypeId: 'integrableId',
    eager: false,
  })
  public integrable?: SiigoIntegration;
}

My repository find looks like:

@PolymorphicRepository(Business)
export class BusinessPolymorphicRepository extends AbstractPolymorphicRepository<Business> {}
private getRepository(): BusinessPolymorphicRepository {
    return AbstractPolymorphicRepository.createRepository(
      this.dataSource,
      BusinessPolymorphicRepository,
    );
  }

public async find(
    value: string,
    key: keyof FindOptionsWhere<Business> = 'id',
  ): Promise<Business | null> {
    return this.getRepository().findOne({
      where: {
        [key]: value,
      },
    });
  }

Throw this exception:

query: SELECT "Business"."id" AS "Business_id", "Business"."created_at" AS "Business_created_at", "Business"."updated_at" AS "Business_updated_at", "Business"."deleted_at" AS "Business_deleted_at", "Business"."name" AS "Business_name", "Business"."active" AS "Business_active", "Business"."integrableType" AS "Business_integrableType", "Business"."integrableId" AS "Business_integrableId" FROM "businesses" "Business" WHERE ( (("Business"."id" = $1)) ) AND ( "Business"."deleted_at" IS NULL ) LIMIT 1 -- PARAMETERS: ["8cc49d2f-a2cf-416f-891b-bc40f21a13bd"]
[AllExceptionsFilter] EntityMetadataNotFoundError: No metadata for "null" was found.
    at DataSource.getMetadata (/Users/macbookpro/Projects/nb/business-ms/src/data-source/DataSource.ts:450:30)
    at Repository.get metadata [as metadata] (/Users/macbookpro/Projects/nb/business-ms/src/repository/Repository.ts:53:40)
    at Repository.findOne (/Users/macbookpro/Projects/nb/business-ms/src/repository/Repository.ts:597:42)
    at BusinessPolymorphicRepository.<anonymous> (/Users/macbookpro/Projects/nb/business-ms/node_modules/typeorm-polymorphic/dist/polymorphic.repository.js:101:68)
    at Generator.next (<anonymous>)
    at /Users/macbookpro/Projects/nb/business-ms/node_modules/typeorm-polymorphic/dist/polymorphic.repository.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/macbookpro/Projects/nb/business-ms/node_modules/typeorm-polymorphic/dist/polymorphic.repository.js:4:12)
    at BusinessPolymorphicRepository.findPolymorphs (/Users/macbookpro/Projects/nb/business-ms/node_modules/typeorm-polymorphic/dist/polymorphic.repository.js:99:16)
    at /Users/macbookpro/Projects/nb/business-ms/node_modules/typeorm-polymorphic/dist/polymorphic.repository.js:85:78

Dependencies:

"@nestjs/typeorm": "^10.0.2",
"typeorm-polymorphic": "^1.0.0"

Note:

I'm setting eager: false in all places and anyway this query the relationship

eliasjtg avatar May 11 '24 16:05 eliasjtg