Skip to content

Broken 'hasMany' relation #425

Closed
Closed

Description

@nvdnkpr

Reproduce this with the loopback-example app:
Modify 'car.json' to have many notes

{
  "name": "Car",
  [ ... all other stuff stays the same ... ]
  "relations": {
    "notes": {
      "type": "hasMany",
      "model": "Note",
      "foreignKey": "carId"
    },
  },
  [ ... all other stuff stays the same ... ]
}

and the Note model to belong to a car

{
  "name": "Note",
  [ ... all other stuff stays the same ... ]
  "relations": {
    "car":{
      "type": "belongsTo",
      "model": "Car",
      "foreignKey": "carId"
    }
  },
  [ ... all other stuff stays the same ... ]
}

Add or change car.js to inspect the prototype to see that the hasMany relation fails, see the lack of get__Notes, create__Notes, delete__Notes

module.exports = function(Car){
  Car.on('dataSourceAttached', function (attachedCar) {
    console.log(attachedCar.prototype);
  });
};

and (to show that belongsTo works) inspect the Note.js:

module.exports = function(Note){
  Note.on('dataSourceAttached', function (attachedNote) {
    console.log(attachedNote.prototype);
  });
};

Activity

  1. raymondfeng commented on Jul 31, 2014

    @raymondfeng
    Member

    Please note that loopback also uses dataSourceAttached event to set up the relations. Maybe your listener is called before the loopback internal ones. Can you check with the api explorer?

  2. nvdnkpr commented on Jul 31, 2014

    @nvdnkpr
    Author

    They relation works in the explorer. Looks like a race condition of listeners, does it ?

  3. nvdnkpr commented on Jul 31, 2014

    @nvdnkpr
    Author

    Perhaps the 'everythingIsAttachedNowYouCanStartDisablingYourMethods' event should be emitted seperately ... and it should be not so long like my proposal 😃

  4. raymondfeng commented on Jul 31, 2014

    @raymondfeng
    Member

    Maybe another event makes sense, for example, dataSourceAttaching and dataSourceAttached.

  5. nvdnkpr commented on Jul 31, 2014

    @nvdnkpr
    Author

    You mean dataSouceAttaching during the and a dataSourceAttached after the final attachment ?
    Sound good, but what for is the dataSourceAttaching needed ?

  6. raymondfeng commented on Jul 31, 2014

    @raymondfeng
    Member

    dataSourceAttaching will be used by loopback to inject the relation methods

  7. nvdnkpr commented on Jul 31, 2014

    @nvdnkpr
    Author

    But why does this only occur on hasMany relations? If its a race condition of injecting relation methods then it should happen with the other relations as well.

  8. raymondfeng commented on Aug 4, 2014

    @raymondfeng
    Member

    It should have been fixed now as loopback internally uses a different event to finish the relation set up.

  9. nvdnkpr commented on Aug 4, 2014

    @nvdnkpr
    Author

    👍 will it be in the next version ?

  10. raymondfeng commented on Aug 4, 2014

    @raymondfeng
    Member

    loopbackio/loopback-datasource-juggler#200

    I thought it was merged and released, but that hasn't happened yet :-)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions