{"action":"created","issue":{"url":"https://api.github.com/repos/digitalsadhu/loopback-component-jsonapi/issues/114","repository_url":"https://api.github.com/repos/digitalsadhu/loopback-component-jsonapi","labels_url":"https://api.github.com/repos/digitalsadhu/loopback-component-jsonapi/issues/114/labels{/name}","comments_url":"https://api.github.com/repos/digitalsadhu/loopback-component-jsonapi/issues/114/comments","events_url":"https://api.github.com/repos/digitalsadhu/loopback-component-jsonapi/issues/114/events","html_url":"https://github.com/digitalsadhu/loopback-component-jsonapi/issues/114","id":134657891,"number":114,"title":"handle hasAndBelongsToMany relations on POST","user":{"login":"behnoodk","id":12370595,"avatar_url":"https://avatars.githubusercontent.com/u/12370595?v=3","gravatar_id":"","url":"https://api.github.com/users/behnoodk","html_url":"https://github.com/behnoodk","followers_url":"https://api.github.com/users/behnoodk/followers","following_url":"https://api.github.com/users/behnoodk/following{/other_user}","gists_url":"https://api.github.com/users/behnoodk/gists{/gist_id}","starred_url":"https://api.github.com/users/behnoodk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/behnoodk/subscriptions","organizations_url":"https://api.github.com/users/behnoodk/orgs","repos_url":"https://api.github.com/users/behnoodk/repos","events_url":"https://api.github.com/users/behnoodk/events{/privacy}","received_events_url":"https://api.github.com/users/behnoodk/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-02-18T18:32:17Z","updated_at":"2016-02-22T13:27:40Z","closed_at":null,"body":"When adding a record through POST which has a `hasAndBelongsToMany` with another model, the component is acting strange. I'm thinking even if this is not supported yet, it should not behave this way:\r\n\r\n```\r\n//loopback models/article.json\r\n{\r\n  \"name\": \"article\",\r\n  \"base\": \"PersistedModel\",\r\n  \"idInjection\": true,\r\n  \"options\": {\r\n    \"validateUpsert\": true\r\n  },\r\n  \"properties\": {\r\n    //....\r\n  },\r\n  \"validations\": [],\r\n  \"relations\": {\r\n    \"category\": {\r\n      \"type\": \"belongsTo\",\r\n      \"model\": \"category\",\r\n      \"foreignKey\": \"\"\r\n    },\r\n    \"tags\": {\r\n      \"type\": \"hasAndBelongsToMany\",\r\n      \"model\": \"tag\"\r\n    }\r\n  },\r\n  \"acls\": [],\r\n  \"methods\": {}\r\n}\r\n\r\n```\r\n```\r\n//Request sent by Ember JSONAdapter\r\nPOST /api/articles\r\n{\r\n  \"data\": {\r\n    \"attributes\": {\r\n      \"title\": \"test\",\r\n      \"slug\": \"test\",\r\n      \"date\": \"Thu Feb 18 2016 17:03:20 GMT+0000\",\r\n      \"text\": \"<p>test</p>\",\r\n      \"posterimageurl\": \"test\"\r\n    },\r\n    \"relationships\": {\r\n      \"tags\": {\r\n        \"data\": [{\r\n          \"type\": \"tags\",\r\n          \"id\": \"4kDzGcCcl\"\r\n        }, {\r\n          \"type\": \"tags\",\r\n          \"id\": \"56c5dbd15b282680a6d89ab0\"\r\n        }]\r\n      },\r\n      \"category\": {\r\n        \"data\": {\r\n          \"type\": \"categories\",\r\n          \"id\": \"41Mi5B35x\"\r\n        }\r\n      }\r\n    },\r\n    \"type\": \"articles\"\r\n  }\r\n}\r\n```\r\n\r\n```\r\n//response given by loopback\r\n{\r\n  \"data\": {\r\n    \"type\": \"articles\",\r\n    \"relationships\": {\r\n      \"category\": {\r\n        \"links\": {\r\n          \"related\": \"http://dev:3000/api/articles/NJDrKxyox/category\"\r\n        },\r\n        \"data\": {\r\n          \"type\": \"categories\",\r\n          \"id\": \"41Mi5B35x\"\r\n        }\r\n      },\r\n      \"tags\": {\r\n        \"links\": {\r\n          \"related\": \"http://dev:3000/api/articles/NJDrKxyox/tags\"\r\n        }\r\n      }\r\n    },\r\n    \"id\": \"NJDrKxyox\",\r\n    \"attributes\": {\r\n      \"title\": \"test\",\r\n      \"slug\": \"test\",\r\n      \"date\": \"2016-02-18T17:03:20.000Z\",\r\n      \"text\": \"<p>test</p>\",\r\n      \"posterimageurl\": \"/api/containers/common/download/179.41eLgvacx.jpg\"\r\n    },\r\n    \"links\": {\r\n      \"self\": \"http://dev:3000/api/articles/NJDrKxyox\"\r\n    }\r\n  }\r\n}\r\n```\r\nSo loopback handles the `belongsTo` relationship correctly. but the `hasAndBelongsToMany` is behaving strange. first there is nothing in the response and second when I check the db, it has updated the field `articleId` of all the included tags to the id of created article. and when a new article is created, this `articleId` gets overwritten for all the tags provided in the request."},"comment":{"url":"https://api.github.com/repos/digitalsadhu/loopback-component-jsonapi/issues/comments/187171960","html_url":"https://github.com/digitalsadhu/loopback-component-jsonapi/issues/114#issuecomment-187171960","issue_url":"https://api.github.com/repos/digitalsadhu/loopback-component-jsonapi/issues/114","id":187171960,"user":{"login":"behnoodk","id":12370595,"avatar_url":"https://avatars.githubusercontent.com/u/12370595?v=3","gravatar_id":"","url":"https://api.github.com/users/behnoodk","html_url":"https://github.com/behnoodk","followers_url":"https://api.github.com/users/behnoodk/followers","following_url":"https://api.github.com/users/behnoodk/following{/other_user}","gists_url":"https://api.github.com/users/behnoodk/gists{/gist_id}","starred_url":"https://api.github.com/users/behnoodk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/behnoodk/subscriptions","organizations_url":"https://api.github.com/users/behnoodk/orgs","repos_url":"https://api.github.com/users/behnoodk/repos","events_url":"https://api.github.com/users/behnoodk/events{/privacy}","received_events_url":"https://api.github.com/users/behnoodk/received_events","type":"User","site_admin":false},"created_at":"2016-02-22T13:27:40Z","updated_at":"2016-02-22T13:27:40Z","body":"I had some other issues with loopback and decided to write my API from scratch with express since it's a small API. Unfortunately I do not have time to do this.\r\nI think at the very least it would be good to mention this in the README. this component does a great job for all belongsTo relationships, but acts unexpectedly for other types of relationships."}}