{"action":"created","issue":{"url":"https://api.github.com/repos/Vincit/objection.js/issues/517","repository_url":"https://api.github.com/repos/Vincit/objection.js","labels_url":"https://api.github.com/repos/Vincit/objection.js/issues/517/labels{/name}","comments_url":"https://api.github.com/repos/Vincit/objection.js/issues/517/comments","events_url":"https://api.github.com/repos/Vincit/objection.js/issues/517/events","html_url":"https://github.com/Vincit/objection.js/issues/517","id":258066634,"number":517,"title":"upsertGraph with compound key in relation","user":{"login":"mattboutet","id":1681861,"avatar_url":"https://avatars0.githubusercontent.com/u/1681861?v=4","gravatar_id":"","url":"https://api.github.com/users/mattboutet","html_url":"https://github.com/mattboutet","followers_url":"https://api.github.com/users/mattboutet/followers","following_url":"https://api.github.com/users/mattboutet/following{/other_user}","gists_url":"https://api.github.com/users/mattboutet/gists{/gist_id}","starred_url":"https://api.github.com/users/mattboutet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mattboutet/subscriptions","organizations_url":"https://api.github.com/users/mattboutet/orgs","repos_url":"https://api.github.com/users/mattboutet/repos","events_url":"https://api.github.com/users/mattboutet/events{/privacy}","received_events_url":"https://api.github.com/users/mattboutet/received_events","type":"User","site_admin":false},"labels":[{"id":199461320,"url":"https://api.github.com/repos/Vincit/objection.js/labels/bug","name":"bug","color":"fc2929","default":true}],"state":"open","locked":false,"assignee":{"login":"koskimas","id":846508,"avatar_url":"https://avatars2.githubusercontent.com/u/846508?v=4","gravatar_id":"","url":"https://api.github.com/users/koskimas","html_url":"https://github.com/koskimas","followers_url":"https://api.github.com/users/koskimas/followers","following_url":"https://api.github.com/users/koskimas/following{/other_user}","gists_url":"https://api.github.com/users/koskimas/gists{/gist_id}","starred_url":"https://api.github.com/users/koskimas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/koskimas/subscriptions","organizations_url":"https://api.github.com/users/koskimas/orgs","repos_url":"https://api.github.com/users/koskimas/repos","events_url":"https://api.github.com/users/koskimas/events{/privacy}","received_events_url":"https://api.github.com/users/koskimas/received_events","type":"User","site_admin":false},"assignees":[{"login":"koskimas","id":846508,"avatar_url":"https://avatars2.githubusercontent.com/u/846508?v=4","gravatar_id":"","url":"https://api.github.com/users/koskimas","html_url":"https://github.com/koskimas","followers_url":"https://api.github.com/users/koskimas/followers","following_url":"https://api.github.com/users/koskimas/following{/other_user}","gists_url":"https://api.github.com/users/koskimas/gists{/gist_id}","starred_url":"https://api.github.com/users/koskimas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/koskimas/subscriptions","organizations_url":"https://api.github.com/users/koskimas/orgs","repos_url":"https://api.github.com/users/koskimas/repos","events_url":"https://api.github.com/users/koskimas/events{/privacy}","received_events_url":"https://api.github.com/users/koskimas/received_events","type":"User","site_admin":false}],"milestone":null,"comments":5,"created_at":"2017-09-15T14:49:58Z","updated_at":"2017-09-16T16:00:13Z","closed_at":null,"author_association":"NONE","body":"It appears that in some cases upsertGraph isn't inserting rows.  Example:\r\n\r\nMy Preferences model was set up with table.primary(['userId', 'category']) and below is what I'm trying:\r\n```javascript\r\nconst prefs = [{ category: 'sms', setting: 'off' }, { category: 'sound', setting: 'off' }];\r\nUsers.query().upsertGraph({ id: user.id, preferences }).asCallback((err, updated) => {\r\n\r\n    console.log(updated);\r\n});\r\n```\r\nWill log:\r\n```\r\nUsers {\r\n  id: 23,\r\n  preferences: \r\n   [ Preferences { category: 'sms', setting: 'nope', userId: 23 },\r\n     Preferences { category: 'loudNoises', setting: 'nope', userId: 23 } ] }\r\n```\r\nIf there's nothing in the table, then adding a `debug()` to the builder chain will show that 2 `select`s are being generated, but no `insert`/`update`s.  If there's data in the table, anything not contained in the `preferences` array will be deleted, but nothing gets inserted.\r\n\r\nIt could be that, because we already have the primary key (`['userId', 'category']`) it's not trying to do the insert but because the row doesn't exist, it's not trying to update?"},"comment":{"url":"https://api.github.com/repos/Vincit/objection.js/issues/comments/329977211","html_url":"https://github.com/Vincit/objection.js/issues/517#issuecomment-329977211","issue_url":"https://api.github.com/repos/Vincit/objection.js/issues/517","id":329977211,"user":{"login":"koskimas","id":846508,"avatar_url":"https://avatars2.githubusercontent.com/u/846508?v=4","gravatar_id":"","url":"https://api.github.com/users/koskimas","html_url":"https://github.com/koskimas","followers_url":"https://api.github.com/users/koskimas/followers","following_url":"https://api.github.com/users/koskimas/following{/other_user}","gists_url":"https://api.github.com/users/koskimas/gists{/gist_id}","starred_url":"https://api.github.com/users/koskimas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/koskimas/subscriptions","organizations_url":"https://api.github.com/users/koskimas/orgs","repos_url":"https://api.github.com/users/koskimas/repos","events_url":"https://api.github.com/users/koskimas/events{/privacy}","received_events_url":"https://api.github.com/users/koskimas/received_events","type":"User","site_admin":false},"created_at":"2017-09-16T16:00:13Z","updated_at":"2017-09-16T16:00:13Z","author_association":"CONTRIBUTOR","body":"I just wrote this and it works...\r\n\r\n```js\r\n  describe('upsertGraph with compound key in relation #403', () => {\r\n    let knex = session.knex;\r\n    let Users;\r\n    let Preferences;\r\n\r\n    before(() => {\r\n      return knex.schema\r\n        .dropTableIfExists('Users')\r\n        .dropTableIfExists('Preferences')\r\n        .createTable('Users', table => {\r\n          table.integer('id').primary();\r\n        })\r\n        .createTable('Preferences', table => {\r\n          table.integer('userId');\r\n          table.string('category');\r\n          table.string('setting');\r\n          table.primary(['userId', 'category']);\r\n        });\r\n    });\r\n\r\n    after(() => {\r\n      return knex.schema\r\n        .dropTableIfExists('Preferences')\r\n        .dropTableIfExists('Users');\r\n    });\r\n\r\n    before(() => {\r\n      Users = class Users extends Model {\r\n        static get tableName() {\r\n          return 'Users';\r\n        }\r\n\r\n        static get relationMappings() {\r\n          return {\r\n            preferences: {\r\n              relation: Model.HasManyRelation,\r\n              modelClass: Preferences,\r\n              join: {\r\n                from: 'Preferences.userId',\r\n                to: 'Users.id'\r\n              }\r\n            }\r\n          };\r\n        }\r\n      };\r\n\r\n      Preferences = class Preferences extends Model {\r\n        static get tableName() {\r\n          return 'Preferences';\r\n        }\r\n\r\n        static get idColumn() {\r\n          return ['userId', 'category'];\r\n        }\r\n      }\r\n\r\n      Users.knex(knex);\r\n      Preferences.knex(knex);\r\n    });\r\n\r\n    before(() => {\r\n      return Users.query().insert({\r\n        id: 1\r\n      })\r\n    });\r\n\r\n    it('test', () => {\r\n      const preferences = [{ category: 'sms', setting: 'off' }, { category: 'sound', setting: 'off' }];\r\n\r\n      return Users\r\n        .query()\r\n        .debug()\r\n        .upsertGraph(\r\n          { id: 1, preferences }\r\n        )\r\n        .then(updated => {\r\n          return Users.query().eager('preferences');\r\n        })\r\n        .then(updated => {\r\n          console.log(updated);\r\n        });\r\n    });\r\n\r\n  });\r\n```"}}
{"actor":{"display_login":"koskimas"}}