Skip to content

Release 0.6.6

Choose a tag to compare

@github-actions github-actions released this 18 Apr 07:00
· 179 commits to main since this release
7726ca7

Bug Fix: findById with includes now correctly resolves @IdAttribute entities

When a child entity uses @IdAttribute on a foreign key (making the child's ID equal to the parent's ID), findById with include would fail to populate the child association — returning undefined instead.

The problem

filterQueryResults distinguished parent from child entities using only id equality. When both share the same ID, the child overwrites the parent in the accumulator and is never added to relatedEntities, so the included association is lost.

// Before: both parent and @IdAttribute child match this condition
if (entityId === res.id) {
  acc.entity = res; // child overwrites parent
}

Example

@Entity
class Vendor extends BaseTable {
  @HasOne(() => Discovery, { foreignKey: 'vendorId' })
  public readonly discovery?: Discovery;
}

@Entity
class Discovery extends BaseTable {
  @IdAttribute
  @ForeignKeyAttribute(() => Vendor, { alias: 'VendorId' })
  public readonly vendorId: ForeignKey<Vendor>;
}

// Before fix: discovery is undefined
// After fix: discovery is correctly populated
const vendor = await Vendor.findById(id, {
  include: [{ association: 'discovery' }]
});

The fix

The parent-detection condition now also checks res.type === this.EntityClass.name, correctly disambiguating parent from child even when IDs match. No behavioral change for entities with distinct IDs.