Skip to content

Conversation

@max-kut
Copy link
Contributor

@max-kut max-kut commented Apr 14, 2024

πŸ”— Linked issue

#1799

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This fix resolves the issue of nested relationships loading incorrectly when the identifiers of both entities are complex.
e.g.

class LeadPointProduct extends Model {
  static entity = 'leads/point/products';
  static primaryKey = ['leadId', 'pointId', 'productId'];

  static fields() {
    return {
      leadId: this.attr(null),
      pointId: this.attr(null),
      productId: this.attr(null),
    };
  }

  declare leadId: number;
  declare pointId: number;
  declare productId: number;
}

class LeadPoint extends Model {
  static entity = 'leads/points';
  static primaryKey = ['leadId', 'pointId'];

  static fields() {
    return {
      leadId: this.attr(null),
      pointId: this.attr(null),
      // relations
      leadPointProducts: this.hasMany(
        LeadPointProduct,
        ['leadId', 'pointId'],
        ['leadId', 'pointId'],
      ).onDelete('cascade'),
    };
  }

  declare leadId: number;
  declare pointId: number;
  declare leadPointProducts: LeadPointProduct[];
}

class Lead extends Model {
 static entity = 'leads';

  static fields() {
    return {
      id: this.attr(null),
      // relations
      leadPoints: this.hasMany(LeadPoint, 'leadId').onDelete('cascade'),
    };
  }

  declare id: number;
  // relations
  declare leadPoints: LeadPoint[];
}

const lead = useRepo(Lead)
      .with('leadPoints', q => q .with('leadPointProducts'))
      .find(1);

Each leadPoints entity had the same array of leadPointProducts elements

This hotfix solves this problem

Reproduce

https://stackblitz.com/edit/pinia-orm-eager-load-bug?file=src%2FApp.vue

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@max-kut
Copy link
Contributor Author

max-kut commented Apr 16, 2024

@CodeDredd please pay attention here. All ready for the patch

@CodeDredd
Copy link
Owner

I try to add an test. The one thing missing :) But good job πŸ‘

@CodeDredd CodeDredd merged commit f109405 into CodeDredd:main May 1, 2024
@github-actions github-actions bot mentioned this pull request May 2, 2024
@max-kut
Copy link
Contributor Author

max-kut commented May 3, 2024

@CodeDredd Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants