{"action":"created","issue":{"url":"https://api.github.com/repos/Vincit/objection.js/issues/266","repository_url":"https://api.github.com/repos/Vincit/objection.js","labels_url":"https://api.github.com/repos/Vincit/objection.js/issues/266/labels{/name}","comments_url":"https://api.github.com/repos/Vincit/objection.js/issues/266/comments","events_url":"https://api.github.com/repos/Vincit/objection.js/issues/266/events","html_url":"https://github.com/Vincit/objection.js/issues/266","id":197225074,"number":266,"title":"$relatedQuery().insert() on a model instance sets id to null","user":{"login":"kyrisu","id":212681,"avatar_url":"https://avatars.githubusercontent.com/u/212681?v=3","gravatar_id":"","url":"https://api.github.com/users/kyrisu","html_url":"https://github.com/kyrisu","followers_url":"https://api.github.com/users/kyrisu/followers","following_url":"https://api.github.com/users/kyrisu/following{/other_user}","gists_url":"https://api.github.com/users/kyrisu/gists{/gist_id}","starred_url":"https://api.github.com/users/kyrisu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kyrisu/subscriptions","organizations_url":"https://api.github.com/users/kyrisu/orgs","repos_url":"https://api.github.com/users/kyrisu/repos","events_url":"https://api.github.com/users/kyrisu/events{/privacy}","received_events_url":"https://api.github.com/users/kyrisu/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":7,"created_at":"2016-12-22T17:27:08Z","updated_at":"2016-12-22T20:16:57Z","closed_at":null,"body":"I'm using the following query to update / insert person address info. \r\n```js\r\n    Person\r\n      .query()\r\n      .patchAndFetchById(id, changes)\r\n      .then(person => {\r\n        if (person.addressId) {\r\n          return person\r\n            .$relatedQuery('address')\r\n            .update(person.address);\r\n        } else {\r\n          return person\r\n            .$relatedQuery('address')\r\n            .insert(brand.address);\r\n        }\r\n      });\r\n```\r\n\r\nThe update (first related query in the example) works, but if the person doesn't have an address I get the following error:\r\n\r\n```\r\nerror: insert into \"addresses\" (\"city\", \"id\") values ($1, $2) returning \"id\" - null value in column \"id\" violates not-null constraint\r\n```\r\n\r\n`changes` variable contains the following info:\r\n```js\r\n{\r\n  name: 'John',\r\n  address: {\r\n    city: 'London'\r\n  }\r\n}\r\n```\r\n\r\nI'm assuming I'm doing very similar query to the one in the [docs](http://vincit.github.io/objection.js/#_s_relatedquery) : \r\n```js\r\njennifer\r\n  .$relatedQuery('pets')\r\n  .insert({species: 'dog', name: 'Fluffy'})\r\n  .then(function (waldo) {\r\n    console.log(waldo.id);\r\n  });\r\n```\r\n\r\nUnfortunately, it doesn't seem to work."},"comment":{"url":"https://api.github.com/repos/Vincit/objection.js/issues/comments/268881782","html_url":"https://github.com/Vincit/objection.js/issues/266#issuecomment-268881782","issue_url":"https://api.github.com/repos/Vincit/objection.js/issues/266","id":268881782,"user":{"login":"koskimas","id":846508,"avatar_url":"https://avatars.githubusercontent.com/u/846508?v=3","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":"2016-12-22T20:16:57Z","updated_at":"2016-12-22T20:16:57Z","body":"Sorry, I missed your previous message. The gist confirmed my suspicion: address is a `HasOneRelation` when it should be a `BelongsToOneRelation` since `Person` has the foreign key."}}
{"actor":{"display_login":"koskimas"}}