Skip to content

Relationship: Many-to-many (self relation) [Followup on issue #1447] #1857

@adm-bome

Description

@adm-bome
          > @CodeDredd: i found out that everything works fine. You got an typo. One time you use "retailerCode" and in the rawData "retailer_code".

My issue was not the retailerCode or retailer_code.

The retailerId and supplierId should be different:

The result supplierId: 1 and retailerId: 16 The index should be [16, 1]

This works great if a developer only uses one belongsToMany relation; like in your test.

class Client extends Model {
  static entity = 'clients'

  static fields () {
    return {
      id: this.number(0),
      name: this.string(null),
      retailers: this.belongsToMany(Client, ClientRetailer, 'supplierId', 'retailerId'),
    }
  }
}

When we add a second "inversed" relation on the Client model. Because when we have a Client and we want to know if the Client has suppliers. Things go wrong.

class Client extends Model {
  static entity = 'clients'

  static fields () {
    return {
      id: this.number(0),
      name: this.string(null),
      retailers: this.belongsToMany(Client, ClientRetailer, 'supplierId', 'retailerId'),
      suppliers: this.belongsToMany(Client, ClientRetailer, 'retailerId', 'supplierId')
    }
  }
}

The retailerId and supplierId should still be different:

The result supplierId: 1 and retailerId: 16 The index should still be [16, 1]
not [16,16]

What bothers me is that in the rawData the relation suppliers the data was never provided.
And it looks like despite the absense of data the relation was still called or filled.

Originally posted by @adm-bome in #1447 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions