{"action":"created","issue":{"url":"https://api.github.com/repos/fortunejs/fortune/issues/237","repository_url":"https://api.github.com/repos/fortunejs/fortune","labels_url":"https://api.github.com/repos/fortunejs/fortune/issues/237/labels{/name}","comments_url":"https://api.github.com/repos/fortunejs/fortune/issues/237/comments","events_url":"https://api.github.com/repos/fortunejs/fortune/issues/237/events","html_url":"https://github.com/fortunejs/fortune/issues/237","id":176935019,"number":237,"title":"Duplicate id's of relationships in database","user":{"login":"Voles","id":1725563,"avatar_url":"https://avatars.githubusercontent.com/u/1725563?v=3","gravatar_id":"","url":"https://api.github.com/users/Voles","html_url":"https://github.com/Voles","followers_url":"https://api.github.com/users/Voles/followers","following_url":"https://api.github.com/users/Voles/following{/other_user}","gists_url":"https://api.github.com/users/Voles/gists{/gist_id}","starred_url":"https://api.github.com/users/Voles/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Voles/subscriptions","organizations_url":"https://api.github.com/users/Voles/orgs","repos_url":"https://api.github.com/users/Voles/repos","events_url":"https://api.github.com/users/Voles/events{/privacy}","received_events_url":"https://api.github.com/users/Voles/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-09-14T15:14:06Z","updated_at":"2016-09-14T16:12:33Z","closed_at":null,"body":"I'm running into an issue where the database contains duplicate id's for relationships. I'm using Ember, the Ember JSONAPIAdapter, Fortune, Fortune JSON API and Fortune Postgres.\r\n\r\nBelow I'll try to describe the issue / reproduction path as good as possible. Please let me know if my assumptions are wrong or there's information missing.\r\n\r\n**The problem**\r\nWhen performing a PATCH request to the server, the relations object is always passed with the request.\r\n\r\nThe relations are updated correctly if it has no inverse property for the related defined. However, when an inverse property for the related model is specified, it will also update the that model.\r\n\r\n**Example data model as defined in the store**\r\n\r\n`````\r\nproduct: {\r\n    name: { type: String },\r\n    shop: { link: 'shop', inverse: 'products' }\r\n  },\r\nshop: {\r\n    name: { type: String },\r\n    products: { link: 'product', inverse: 'shop', isArray: true }\r\n  },\r\n`````\r\n\r\n**Reproduction path**\r\n\r\n1. I'm sending a PATCH request for the product with id `product-1`. Only the name has changed.\r\nThe request will contain the `relationships` property with the following value for the `shop` property:  \r\n`data: { id: shop-1, type: 'shop' }`  \r\nI'm not sure if the relationship is supposed to be specified in this request, but that's how Ember's JSONAPISerializer does it.\r\n\r\n2. The Fortune PostgreSQL adapter will run the `update()` method for the product model. Next it will run `update()` for the shop model.  \r\nBecause it's an array it will execute the following line: `set.push(`\"${field}\" = array_append(\"${field}\", $${index})`)` See https://github.com/fortunejs/fortune-postgres/blob/master/lib/index.js#L408\r\n\r\n3. However, there seems to be no check if that id is already present in the array. The result is that the array can contain duplicates.\r\n\r\n4. Later on, when updating the shop-1 model, the request fails because of duplicate id's inside the `products` property.  \r\n\r\n> Unhandled rejection ConflictError: Duplicate ID \"product-1\" in the field \"products\".\r\n\r\n**Conclusion**\r\nI hope I've described my problem clear enough. Looking forward to have your feedback. If I have a better understanding of what is going wrong, I can work on a fix.\r\n\r\nMany thanks in advance!"},"comment":{"url":"https://api.github.com/repos/fortunejs/fortune/issues/comments/247066711","html_url":"https://github.com/fortunejs/fortune/issues/237#issuecomment-247066711","issue_url":"https://api.github.com/repos/fortunejs/fortune/issues/237","id":247066711,"user":{"login":"0x8890","id":1175095,"avatar_url":"https://avatars.githubusercontent.com/u/1175095?v=3","gravatar_id":"","url":"https://api.github.com/users/0x8890","html_url":"https://github.com/0x8890","followers_url":"https://api.github.com/users/0x8890/followers","following_url":"https://api.github.com/users/0x8890/following{/other_user}","gists_url":"https://api.github.com/users/0x8890/gists{/gist_id}","starred_url":"https://api.github.com/users/0x8890/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/0x8890/subscriptions","organizations_url":"https://api.github.com/users/0x8890/orgs","repos_url":"https://api.github.com/users/0x8890/repos","events_url":"https://api.github.com/users/0x8890/events{/privacy}","received_events_url":"https://api.github.com/users/0x8890/received_events","type":"User","site_admin":false},"created_at":"2016-09-14T16:12:33Z","updated_at":"2016-09-14T16:12:33Z","body":"Most likely there is an incorrect calculation done somewhere in here: https://github.com/fortunejs/fortune/blob/master/lib/dispatch/update.js\r\n\r\nWhat it does is try to update only what is necessary, and it seems there is something wrong because if the field is the same it shouldn't try to update the relationship.\r\n\r\nIf it isn't a core bug, then what I suspect is the issue is JSON API's requirement of string IDs isn't working with number IDs in Postgres."}}
{"actor":{"display_login":"0x8890"}}