js-data icon indicating copy to clipboard operation
js-data copied to clipboard

HasOne `findExistingLinksFor` has improper method signature

Open jayeb opened this issue 6 years ago • 2 comments

Description

The "parent" record on a hasOne/belongsTo relationship doesn't appear to link correctly, even when the child record already exists in the datastore. I think this is due to the findExistingLinksFor method defined on the HasOne relationship class having an improper method signature.

Environment

  • js-data version: 3.0.2

Steps to reproduce

The HasOne.findExistingLinksFor method is looking for two arguments:

findExistingLinksFor (relatedMapper, record) { ...

But the HasMany.findExistingLinksFor and BelongsTo.findExistingLinksFor methods both look for two arguments:

findExistingLinksFor (record) { ...

Best I can tell, this method is only ever called in one place (Line 146 of Relation.js), and the arguments passed in match the HasMany and BelongsTo method signatures:

relatedData = this.findExistingLinksFor(record)

I forked the repo and tried this locally:

findExistingLinksFor (record) {
  const relatedMapper = this.getRelation()
  const recordId = utils.get(record, relatedMapper.idAttribute)
  const records = this.findExistingLinksByForeignKey(recordId)

  if (records && records.length) {
    return records[0]
  }
},

But apparently this.getRelation() chokes unless the child record's Mapper was defined before the parent record's Mapper. I think I've taken this about as far as I can at this point, hoping someone with a better working knowledge of the inner mechanisms here can swoop in and help out.

jayeb avatar Mar 28 '19 04:03 jayeb

@jayeb failing test?

crobinson42 avatar Mar 28 '19 23:03 crobinson42

Or if there’s not test hitting this logic case can you write one to help resolve?

crobinson42 avatar Mar 28 '19 23:03 crobinson42