{"action":"created","issue":{"url":"https://api.github.com/repos/Vincit/objection.js/issues/58","labels_url":"https://api.github.com/repos/Vincit/objection.js/issues/58/labels{/name}","comments_url":"https://api.github.com/repos/Vincit/objection.js/issues/58/comments","events_url":"https://api.github.com/repos/Vincit/objection.js/issues/58/events","html_url":"https://github.com/Vincit/objection.js/issues/58","id":124223144,"number":58,"title":"Insert method returns 0 for non-generated primary key","user":{"login":"jsneden","id":817098,"avatar_url":"https://avatars.githubusercontent.com/u/817098?v=3","gravatar_id":"","url":"https://api.github.com/users/jsneden","html_url":"https://github.com/jsneden","followers_url":"https://api.github.com/users/jsneden/followers","following_url":"https://api.github.com/users/jsneden/following{/other_user}","gists_url":"https://api.github.com/users/jsneden/gists{/gist_id}","starred_url":"https://api.github.com/users/jsneden/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jsneden/subscriptions","organizations_url":"https://api.github.com/users/jsneden/orgs","repos_url":"https://api.github.com/users/jsneden/repos","events_url":"https://api.github.com/users/jsneden/events{/privacy}","received_events_url":"https://api.github.com/users/jsneden/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-12-29T15:08:16Z","updated_at":"2015-12-29T17:39:27Z","closed_at":null,"body":"I have a table, `entities` which has a primary key of a generated UUID. I'd like for my services to not have to worry about this PK generation when creating a new `Entity`. Therefore, I added a `beforeInsert` method to generate the UUID and add it to the model. This much works, but I can't seem to find a way to return the generated UUID to the invoking service.\r\n\r\nAccording to the documentation, the identifier should be returned with the insert():\r\n> NOTE: The return value of the insert query only contains the properties given to the insert method plus the identifier.\r\n\r\nUnfortunately, the returned identifier is `0`:\r\n```\r\n{ method: 'insert',\r\n  options: {},\r\n  bindings: [ '7ffecfde-ac66-49a9-a3f8-06be0f3fa61e', 'COMPANY' ],\r\n  sql: 'insert into `entities` (`entity_id`, `entity_type`) values (?, ?)' }\r\nTHIS: {\"entityType\":\"COMPANY\",\"entityId\":0}\r\n```\r\n\r\nEven if I use insertAndFetch(), I still cannot retrieve the generated UUID, because the select lookup fails when it tries to lookup the `entity` with a PK of `0`.\r\n\r\n**service.js:**\r\n```javascript\r\n...\r\nreturn Entity\r\n\t.bindTransaction(transaction)\r\n\t.query()\r\n\t.insert({entityType: 'COMPANY'});\r\n...\r\n```\r\n\r\n**entity.js:**\r\n```javascript\r\n...\r\nEntity.tableName = 'entities';\r\nEntity.idColumn = 'entity_id';\r\n\r\nEntity.prototype.$beforeInsert = function (context) {\r\n\tthis.entityId = uuid.v4();\r\n};\r\n\r\nEntity.prototype.$afterInsert = function (context) {\r\n\tconsole.log('THIS: ' + JSON.stringify(this));\r\n};\r\n\r\nEntity.jsonSchema = {\r\n\ttype: 'object',\r\n\trequired: ['entityType'],\r\n\r\n\tproperties: {\r\n\t\tentityId: {type: 'string', minLength: 36, maxLength: 36},\r\n\t\tentityType: {type: 'string', minLength: 1, maxLength: 10}\r\n\t}\r\n};\r\n...\r\n```\r\n\r\n**entity table (MySQL):**\r\n```sql\r\nCREATE TABLE `entities` (\r\n  `entity_id` varchar(36) NOT NULL,\r\n  `entity_type` varchar(10) NOT NULL,\r\n  PRIMARY KEY (`entity_id`),\r\n  KEY `fk_entities_entity_type1_idx` (`entity_type`),\r\n  CONSTRAINT `entities_ibfk_1` FOREIGN KEY (`entity_type`) REFERENCES `entity_types` (`entity_type`)\r\n) ENGINE=InnoDB AUTO_INCREMENT=186 DEFAULT CHARSET=utf8;\r\n```"},"comment":{"url":"https://api.github.com/repos/Vincit/objection.js/issues/comments/167839408","html_url":"https://github.com/Vincit/objection.js/issues/58#issuecomment-167839408","issue_url":"https://api.github.com/repos/Vincit/objection.js/issues/58","id":167839408,"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":"2015-12-29T17:39:27Z","updated_at":"2015-12-29T17:39:27Z","body":"I'm not aware of the problem you described. Could you provide some more information on that?"}}