Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self-Referent Relationship in Save Response Blows Up Save #7265

Closed
davelindquist-egistix opened this issue Aug 11, 2020 · 3 comments
Closed

Comments

@davelindquist-egistix
Copy link

Reproduction

See the Ember Twiddle at:

https://ember-twiddle.com/5fd0892440b64337440c13d4317ed295

The component provides a 'Save' button and instructions (clicking the 'Save' will immediately cause the error).

Description

We have a case where we:

  • Create a new record
  • Fill in a few fields
  • Save
  • The backend returns the record, with a relationship filled in
  • This relationship is self-referential (has its own id)
  • Ember Data blows up, complaining that the id is already in the store.

For example, our model looks something like this:

export default class extends Model {
  @attr("string") name
  @belongsTo('company', {inverse: null} ) parent
}

and the save returns data something like this:

  data: {
    type: "company",
    id: 1,
    attributes: {
      "name": "Test Company"
    },
    relationships: {
      parent: {
        data: {
          type: "company",
          id: 1
        }
      }
    }
  }

The Ember Data sequence of events appears to be:

  1. Parse the relationships
  2. Create a 'placeholder' company model for the relationship with id of "1", and put it into the map
  3. Parse the object itself
  4. Check to see if that id is already used (and is not myself) - var existingInternalModel = this.peekIdOnly(modelName, id);
  5. Blow up, because it found the "id 1" item created for the relationship, which is not 'me'.

Versions

Since we do not use yarn, we cannot run the indicated commands to get versions - however, here is what package.json/package-lock.json has to say about the packages of interest:

    "ember-cli": "3.12.1",
    "ember-source": "3.12.3",
    "ember-data": "3.12.6",
davelindquist-egistix added a commit to davelindquist-egistix/data that referenced this issue Aug 11, 2020
…ent relationship in save response blows up)
davelindquist-egistix added a commit to davelindquist-egistix/data that referenced this issue Aug 11, 2020
…in Save Response Blows Up Save).

Tests the case where a save-response returns a relationship that includes the self-id.
This passes in master, but is broken in 3.12 LTS
@runspired
Copy link
Contributor

runspired commented Apr 14, 2021

This can be resolved in your app by reflecting the record identifier's lid back in the payload at each location this particular record identity (type id pair) appears. I'd suggest reading the RFC for documentation (it was fully implemented) or the test suite which covers this scenario.

RFC: https://github.com/emberjs/rfcs/blob/master/text/0403-ember-data-identifiers.md

Tests: https://github.com/emberjs/data/blob/master/packages/-ember-data/tests/integration/identifiers/lid-reflection-test.ts

@runspired
Copy link
Contributor

Going to reopen this since the proposed fix was simple enough it seems ok to add so that fewer folks need to worry about adding lid for a case we can potentially trivially cover.

@runspired runspired reopened this Apr 14, 2021
@runspired
Copy link
Contributor

Closing since this works on master without the need for reflecting the lid back.

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

No branches or pull requests

2 participants