{"action":"created","issue":{"url":"https://api.github.com/repos/balderdashy/waterline/issues/1347","repository_url":"https://api.github.com/repos/balderdashy/waterline","labels_url":"https://api.github.com/repos/balderdashy/waterline/issues/1347/labels{/name}","comments_url":"https://api.github.com/repos/balderdashy/waterline/issues/1347/comments","events_url":"https://api.github.com/repos/balderdashy/waterline/issues/1347/events","html_url":"https://github.com/balderdashy/waterline/issues/1347","id":150965081,"node_id":"MDU6SXNzdWUxNTA5NjUwODE=","number":1347,"title":"Associations lost when saving a new association record","user":{"login":"tobalsgithub","id":1554930,"node_id":"MDQ6VXNlcjE1NTQ5MzA=","avatar_url":"https://avatars3.githubusercontent.com/u/1554930?v=4","gravatar_id":"","url":"https://api.github.com/users/tobalsgithub","html_url":"https://github.com/tobalsgithub","followers_url":"https://api.github.com/users/tobalsgithub/followers","following_url":"https://api.github.com/users/tobalsgithub/following{/other_user}","gists_url":"https://api.github.com/users/tobalsgithub/gists{/gist_id}","starred_url":"https://api.github.com/users/tobalsgithub/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tobalsgithub/subscriptions","organizations_url":"https://api.github.com/users/tobalsgithub/orgs","repos_url":"https://api.github.com/users/tobalsgithub/repos","events_url":"https://api.github.com/users/tobalsgithub/events{/privacy}","received_events_url":"https://api.github.com/users/tobalsgithub/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":10,"created_at":"2016-04-25T19:56:21Z","updated_at":"2018-08-07T14:16:44Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"<!--\n======================================================\nHELLO, and welcome to the (experimental) Sailsbot\nissue-submission system.  If you encounter any\nproblems with this system, please contact\nsgress454@treeline.io\n\nFIRST THINGS FIRST: please fill out the following info \n(Don't change text between **) \n======================================================\n### BEGIN VERSION INFO ###\n-->\n\n**Waterline version**: 0.10.26\n**Node version**: v0.12.2\n**NPM version**: 2.13.0\n**Operating system**: OSX 10.10.5\n**Adapter**: sails-postgresql 0.10.17\n\n<!-- \n### END VERSION INFO ###\n======================================================\nGREAT! - Now read carefully \n(Sailsbot will know if you don't)!  \n======================================================\n\nBefore you submit an issue for Waterline, please make sure you have read the Sails issue contribution guide (http://bit.ly/sails-issue-guide) carefully, and then verify the following by typing an X in between each set of [ ] brackets below (don't make any other changes to the text!):\n\n### BEGIN PLEDGE ###\n- [x] I am experiencing a concrete technical issue with Waterline (ideas and feature proposals should follow the guide for proposing features and enhancements (http://bit.ly/sails-feature-guide), which involves making a pull request).  If you're not 100% certain whether it's a bug or not, that's okay--you may continue.  The worst that can happen is that the issue will be closed and we'll point you in the right direction.\n\n- [x] I am not asking a question about how to use Waterline or about whether or not Waterline has a certain feature (please refer to the documentation(http://sailsjs.org), or post on http://stackoverflow.com, our Google Group (http://bit.ly/sails-google-group) or our live chat (https://gitter.im/balderdashy/sails).\n\n- [x] I have already searched for related issues, and found none open (if you found a related _closed_ issue, please link to it in your post).\n\n- [x] My issue title is concise, on-topic and polite.\n\nand finally...\n- [x] **I can provide steps to reproduce this issue that others can follow.**\n\nIdeally, this involves creating a new repo that demonstrates the problem (see instructions at http://bit.ly/waterline-issue-repo).  Even though your issue may seem so simple to reproduce that a new repo is unnecessary, you'd be surprised how many solutions present themselves when you start from `sails new` and attempt to recreate your issue from scratch in a new app.  This ensures that the real issue isn't in your user code or in a third-party module.  If you're absolutely _convinced_ that a new repo is unnecessary, provide clear, concise and _specific_ steps to reproduce the problem in your post (not \"create a model then do blueprint create\").\n### END PLEDGE ###\n\nOKAY--THANKS FOR READING!  Continue posting details of your issue below. -->\n\n<hr/>\n\nWe have two models in a one to many association. Message -> Transmission.\n\nTransmission.js\n\n``` js\nmodule.exports = {\n  attributes: {\n    transmissions: {\n      collection: 'Transmission',\n      via: 'message'\n    }\n};\n```\n\nMessage.js\n\n``` js\nmodule.exports = {\n  attributes: {\n    message: {\n      model: 'Message',\n    }\n};\n```\n\nFollowing the below code, I can create a message, create a transmission attached to the message, create another transmission attached to the message, and then lose the link between the first transmission and the message on saving the second transmission. This happens only if message is populated on the second transmission when we save it. \n\nThe following script should be run line by line at the sails console. \n\nBasically, it does the following:\n- Creates a message\n- Creates a first transmission linked to the message\n- Creates a second transmission linked to the same message\n- Populates the message on the second transmission by directly setting the message on the second transmission\n- Save the second transmission\n- Reload the first transmission from the db\n- Note that the link to the message is gone from the first transmission\n\n``` js\nvar m;\n\nMessage.create({}, function (err, msg) { m = msg; });\n\nvar t1;\n\nTransmission.create({ message: m.id }, function (err, t) { t1 = t; });\n\nTransmission.findOne(t1.id, function (err, t) { t1 = t; });\n\nt1.message; // equals m.id - All good\n\n// Now we create a second Transmission\n\nvar t2;\n\nTransmission.create({ message: m.id }, function (err, t) { t2 = t; });\n\nt2.message = m; // add populated message to t2\n\nt2.save(console.log);\n\nt2.message; // equals m\n\nt1.message; // equals m.id - still there since we haven't reloaded from db\n\nTransmission.findOne(t1.id, function (err, t) { t1 = t; });\n\nt1.message; // null\n```\n\nNote that this does NOT happen if we do not directly set the message on the second transmission. For example, if, instead of setting the message on `t2` like so `t2.message = m`, but rather we loaded `t2` from the db and used the `.populate` function to populate the message on `t2`, then the problem does not occur on `t1`. \n\nAlso, this only happens for one to many assocations on the message model. In our code, both Message and Transmission have many more associations. Both have one-to-many and many-to-one associations. However, as far as I can tell, only `one-to-many` associations on the Message model are affected by this issue.\n\nThere are a number of issues out there related to associations acting weird on save, however it's not clear that any of them are this particular issue. \n#975\n#803\n#1031\n#656\n"},"comment":{"url":"https://api.github.com/repos/balderdashy/waterline/issues/comments/411072090","html_url":"https://github.com/balderdashy/waterline/issues/1347#issuecomment-411072090","issue_url":"https://api.github.com/repos/balderdashy/waterline/issues/1347","id":411072090,"node_id":"MDEyOklzc3VlQ29tbWVudDQxMTA3MjA5MA==","user":{"login":"mikermcneil","id":618009,"node_id":"MDQ6VXNlcjYxODAwOQ==","avatar_url":"https://avatars3.githubusercontent.com/u/618009?v=4","gravatar_id":"","url":"https://api.github.com/users/mikermcneil","html_url":"https://github.com/mikermcneil","followers_url":"https://api.github.com/users/mikermcneil/followers","following_url":"https://api.github.com/users/mikermcneil/following{/other_user}","gists_url":"https://api.github.com/users/mikermcneil/gists{/gist_id}","starred_url":"https://api.github.com/users/mikermcneil/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mikermcneil/subscriptions","organizations_url":"https://api.github.com/users/mikermcneil/orgs","repos_url":"https://api.github.com/users/mikermcneil/repos","events_url":"https://api.github.com/users/mikermcneil/events{/privacy}","received_events_url":"https://api.github.com/users/mikermcneil/received_events","type":"User","site_admin":false},"created_at":"2018-08-07T14:16:44Z","updated_at":"2018-08-07T14:16:44Z","author_association":"MEMBER","body":"Seen this in Sails 1?"}}
{"actor":{"display_login":"mikermcneil"}}