{"action":"created","issue":{"url":"https://api.github.com/repos/neumino/thinky/issues/355","labels_url":"https://api.github.com/repos/neumino/thinky/issues/355/labels{/name}","comments_url":"https://api.github.com/repos/neumino/thinky/issues/355/comments","events_url":"https://api.github.com/repos/neumino/thinky/issues/355/events","html_url":"https://github.com/neumino/thinky/issues/355","id":108109434,"number":355,"title":"saveAll() doesn't save grandchildren models after the first child model entry","user":{"login":"subblue","id":3010,"avatar_url":"https://avatars.githubusercontent.com/u/3010?v=3","gravatar_id":"","url":"https://api.github.com/users/subblue","html_url":"https://github.com/subblue","followers_url":"https://api.github.com/users/subblue/followers","following_url":"https://api.github.com/users/subblue/following{/other_user}","gists_url":"https://api.github.com/users/subblue/gists{/gist_id}","starred_url":"https://api.github.com/users/subblue/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/subblue/subscriptions","organizations_url":"https://api.github.com/users/subblue/orgs","repos_url":"https://api.github.com/users/subblue/repos","events_url":"https://api.github.com/users/subblue/events{/privacy}","received_events_url":"https://api.github.com/users/subblue/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-09-24T11:06:44Z","updated_at":"2015-09-29T02:18:45Z","closed_at":null,"body":"I want to import blocks of data that has a fairly typical relationship:\r\nAn artist has many albums and each album has many tracks.\r\n\r\nThe data is coming in as a single nested object for each artist and I want to use saveAll() to save this out into their respective models. However, I'm finding that while the artist and album records are being saved only the tracks from the first album are also being saved. The tracks from the rest of the albums aren't persisting.\r\n\r\nHere is a self-contained example that will create the tables and save data to the ```test``` database. \r\n\r\n```javascript\r\n'use strict';\r\n\r\nvar thinky = require('thinky')({\r\n  host: 'localhost', \r\n  port: 28015, \r\n  db:   'test'\r\n});\r\nvar type = thinky.type;\r\nvar r = thinky.r;\r\n\r\n// example data structure\r\nvar data = {\r\n  name: 'Artist name',\r\n  albums: [\r\n    {\r\n      title: 'Album 1',\r\n      tracks: [\r\n        {title: 'Track title 1-1'},\r\n        {title: 'Track title 1-2'},\r\n        {title: 'Track title 1-3'},\r\n        {title: 'Track title 1-4'},\r\n        {title: 'Track title 1-5'}\r\n      ]\r\n    },\r\n    {\r\n      title: 'Album 2',\r\n      tracks: [\r\n        {title: 'Track title 2-1'},\r\n        {title: 'Track title 2-2'},\r\n        {title: 'Track title 2-3'},\r\n        {title: 'Track title 2-4'},\r\n        {title: 'Track title 2-5'},\r\n        {title: 'Track title 2-6'},\r\n        {title: 'Track title 2-7'}\r\n      ]\r\n    },\r\n    {\r\n      title: 'Album 3',\r\n      tracks: [\r\n        {title: 'Track title 3-1'},\r\n        {title: 'Track title 3-2'},\r\n        {title: 'Track title 3-3'}\r\n      ]\r\n    }\r\n  ]\r\n};\r\n\r\n// define models\r\nvar Artist = thinky.createModel('artists', {\r\n  id:        type.string(),\r\n  name:      type.string()\r\n});\r\n\r\nvar Album = thinky.createModel('albums', {\r\n  id:        type.string(),\r\n  artistId:  type.string(),\r\n  title:     type.string()\r\n});\r\n\r\nvar Track = thinky.createModel('tracks', {\r\n  id:        type.string(),\r\n  albumId:   type.string(),\r\n  title:     type.string()\r\n});\r\n\r\n// define relationships\r\nArtist.hasMany(Album, 'albums', 'id', 'artistId');\r\nAlbum.belongsTo(Artist, 'artist', 'artistId', 'id');\r\n\r\nAlbum.hasMany(Track, 'tracks', 'id', 'albumId');\r\nTrack.belongsTo(Album, 'album', 'albumId', 'id');\r\n\r\n// create artist and save with all related models\r\nvar artist = new Artist(data);\r\nartist.saveAll()\r\n  .then(function () {\r\n    // check the number of albums created\r\n    return r.table('albums').count().run();\r\n  })\r\n  .then(function (num) {\r\n    console.log('total album count should equal 3:', num === 3, num);\r\n    \r\n    // check the number of tracks created\r\n    return r.table('tracks').count().run();\r\n  })\r\n  .then(function (num) {\r\n    console.log('total track count should equal 15:', num === 15, num);\r\n  })\r\n  .catch(function (err) {\r\n    console.log('Error saving artist:', err);\r\n  })\r\n  .finally(function () {\r\n    process.exit(1);\r\n  });\r\n```\r\n\r\nIt will create an artist, 3 albums but only 5 track entries instead of the 15 that are defined in the example data structure.\r\n\r\nIs there something I'm missing in how I use saveAll() ?\r\n(for the real data there are many more fields for each album and track, which is why I'm normalising the structure like this instead of storing it all in a single document for each artist).\r\n\r\nThanks!\r\n"},"comment":{"url":"https://api.github.com/repos/neumino/thinky/issues/comments/143925090","html_url":"https://github.com/neumino/thinky/issues/355#issuecomment-143925090","issue_url":"https://api.github.com/repos/neumino/thinky/issues/355","id":143925090,"user":{"login":"neumino","id":1461947,"avatar_url":"https://avatars.githubusercontent.com/u/1461947?v=3","gravatar_id":"","url":"https://api.github.com/users/neumino","html_url":"https://github.com/neumino","followers_url":"https://api.github.com/users/neumino/followers","following_url":"https://api.github.com/users/neumino/following{/other_user}","gists_url":"https://api.github.com/users/neumino/gists{/gist_id}","starred_url":"https://api.github.com/users/neumino/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/neumino/subscriptions","organizations_url":"https://api.github.com/users/neumino/orgs","repos_url":"https://api.github.com/users/neumino/repos","events_url":"https://api.github.com/users/neumino/events{/privacy}","received_events_url":"https://api.github.com/users/neumino/received_events","type":"User","site_admin":false},"created_at":"2015-09-29T02:18:45Z","updated_at":"2015-09-29T02:18:45Z","body":"`saveAll` without argument is deprecated, and doesn't recurse if it finds a model it already saved.\r\n\r\nReplace it with\r\n```\r\nartist.saveAll({albums: {tracks: true}})\r\n```\r\nAnd it should work."}}