{"action":"created","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/6669","repository_url":"https://api.github.com/repos/sequelize/sequelize","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/6669/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/6669/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/6669/events","html_url":"https://github.com/sequelize/sequelize/issues/6669","id":181572922,"number":6669,"title":"Using Association to create child records.","user":{"login":"khushroo-mistry","id":2703762,"avatar_url":"https://avatars.githubusercontent.com/u/2703762?v=3","gravatar_id":"","url":"https://api.github.com/users/khushroo-mistry","html_url":"https://github.com/khushroo-mistry","followers_url":"https://api.github.com/users/khushroo-mistry/followers","following_url":"https://api.github.com/users/khushroo-mistry/following{/other_user}","gists_url":"https://api.github.com/users/khushroo-mistry/gists{/gist_id}","starred_url":"https://api.github.com/users/khushroo-mistry/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/khushroo-mistry/subscriptions","organizations_url":"https://api.github.com/users/khushroo-mistry/orgs","repos_url":"https://api.github.com/users/khushroo-mistry/repos","events_url":"https://api.github.com/users/khushroo-mistry/events{/privacy}","received_events_url":"https://api.github.com/users/khushroo-mistry/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-10-07T02:32:28Z","updated_at":"2016-10-10T01:19:14Z","closed_at":"2016-10-07T10:23:53Z","body":"## What you are doing?\r\nModel:\r\n\r\n     'use strict';\r\n     var Sequelize = require(\"sequelize\");\r\n     exports.getLivingExpenseModelDefinition = function(sequelize) {\r\n       return getLivingExpensesDef(sequelize);\r\n     };\r\n\r\n      exports.getOpportunityModelDefinition = function(sequelize) {\r\n       return getOpportunityModelDef(sequelize)\r\n      };\r\n\r\n       var getOpportunityModelDef = function(sequelize) {\r\n       var opportunityDefinition = sequelize.define('opportunity', {\r\n\r\n        //The primary key\r\n        id: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},\r\n\r\n        //The description\r\n        description: {type: Sequelize.STRING, allowNull: false}\r\n       });\r\n\r\n       var livingExpensesDef = getLivingExpensesDef(sequelize);\r\n\r\n    opportunityDefinition.knownAssociations = [\r\n        livingExpensesDef\r\n    ];\r\n\r\n    opportunityDefinition.hasMany(livingExpensesDef, {foreignKey: 'opportunity_id'});\r\n    livingExpensesDef.belongsTo(opportunityDefinition, {foreignKey: 'opportunity_id'});\r\n\r\n\r\n        return opportunityDefinition;\r\n\r\n    };\r\n\r\n    var getLivingExpensesDef = function (sequelize) {\r\n\r\n    var livinExpDef =  sequelize.define('living_expenses', {\r\n\r\n        //The primary key\r\n        id: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},\r\n\r\n        //The description\r\n        description: {type: Sequelize.STRING, allowNull: false},\r\n\r\n        //The value\r\n        value: {type: Sequelize.INTEGER, allowNull: false},\r\n\r\n        //The opportunity id as the FK\r\n        opportunityId: {\r\n            type: Sequelize.INTEGER,\r\n            field: \"opportunity_id\",\r\n            allowNull: false,\r\n            references: {\r\n                model: \"opportunity\",\r\n                key: \"id\"\r\n            }\r\n        }\r\n\r\n    });\r\n\r\n    //var optyDef = getOpportunityModelDef(sequelize);\r\n      return livinExpDef;\r\n\r\n     };\r\n\r\nMethod using the model:\r\n        var createLivingExpensesForOpportunity = function(opportunityId) {\r\n\r\n        var opportunity = opportunityModel.getOpportunityModelDefinition(sequelize);\r\n        var le = opportunityModel.getLivingExpenseModelDefinition(sequelize);\r\n        var builtLe = le.build({description: \"something\", value:500, opportunityId:1});\r\n\r\n        return opportunity.findById(opportunityId, {include: opportunity.knownAssociations})\r\n            .then(function(opty){\r\n\r\n                opty.setLiving_expenses([builtLe])\r\n                    .then(function(le){\r\n\r\n                        return Promise.resolve('saved');\r\n                    })\r\n                    .catch(function(error){\r\n                        console.log(error);\r\n                        return Promise.reject(error);\r\n                    });\r\n\r\n            })\r\n            .catch(function(error){\r\n                console.log(error);\r\n                return Promise.reject(error);\r\n            });\r\n\r\n    };\r\n\r\n## What do you expect to happen?\r\nI expect the child record for an opportunity object to be saved.\r\n\r\n## What is actually happening?\r\nGetting the follow error:\r\nExecuting (default): UPDATE `living_expenses` SET `opportunity_id`=NULL WHERE `id` IN (1, 2, 3, 4, 5, 6, 7)\r\n[TypeError: val.replace is not a function]\r\nUnhandled rejection TypeError: val.replace is not a function\r\n    at Object.SqlString.escape (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/lib/sql-string.js:63:15)\r\n    at Object.QueryGenerator.escape (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/lib/dialects/abstract/query-generator.js:979:22)\r\n    at /Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2280:23\r\n    at Array.map (native)\r\n    at Object.QueryGenerator.whereItemQuery (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/lib/dialects/abstract/query-generator.js:2279:49)\r\n    at /Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1980:25\r\n    at /Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/node_modules/lodash/lodash.js:4389:15\r\n    at baseForOwn (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/node_modules/lodash/lodash.js:2652:24)\r\n    at Function.forOwn (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/node_modules/lodash/lodash.js:12254:24)\r\n    at Object.QueryGenerator.whereItemsQuery (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1979:9)\r\n    at Object.QueryGenerator.whereQuery (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1951:22)\r\n    at Object.QueryGenerator.updateQuery (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/lib/dialects/abstract/query-generator.js:482:19)\r\n    at QueryInterface.bulkUpdate (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/lib/query-interface.js:604:33)\r\n    at /Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/lib/model.js:2589:32\r\n    at tryCatcher (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/node_modules/bluebird/js/release/util.js:16:23)\r\n    at Promise._settlePromiseFromHandler (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:510:31)\r\n    at Promise._settlePromise (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:567:18)\r\n    at Promise._settlePromise0 (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:612:10)\r\n    at Promise._settlePromises (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:691:18)\r\n    at Async._drainQueue (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/node_modules/bluebird/js/release/async.js:138:16)\r\n    at Async._drainQueues (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/node_modules/bluebird/js/release/async.js:148:10)\r\n    at Immediate.Async.drainQueues [as _onImmediate] (/Library/WebServer/Documents/opportunitymodeldao/node_modules/sequelize/node_modules/bluebird/js/release/async.js:17:14)\r\n    at processImmediate [as _immediateCallback] (timers.js:383:17)\r\n\r\n\r\n__Dialect:__ mysql\r\n__Database version:__ latest\r\n__Sequelize version:__ ^3.24.3\r\n\r\nNote: This is POC code so it may not be very well structured. I have spent quite sometime chasing this issue and would really appreciate some support with this issue.\r\n"},"comment":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/comments/252525938","html_url":"https://github.com/sequelize/sequelize/issues/6669#issuecomment-252525938","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/6669","id":252525938,"user":{"login":"khushroo-mistry","id":2703762,"avatar_url":"https://avatars.githubusercontent.com/u/2703762?v=3","gravatar_id":"","url":"https://api.github.com/users/khushroo-mistry","html_url":"https://github.com/khushroo-mistry","followers_url":"https://api.github.com/users/khushroo-mistry/followers","following_url":"https://api.github.com/users/khushroo-mistry/following{/other_user}","gists_url":"https://api.github.com/users/khushroo-mistry/gists{/gist_id}","starred_url":"https://api.github.com/users/khushroo-mistry/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/khushroo-mistry/subscriptions","organizations_url":"https://api.github.com/users/khushroo-mistry/orgs","repos_url":"https://api.github.com/users/khushroo-mistry/repos","events_url":"https://api.github.com/users/khushroo-mistry/events{/privacy}","received_events_url":"https://api.github.com/users/khushroo-mistry/received_events","type":"User","site_admin":false},"created_at":"2016-10-10T01:19:14Z","updated_at":"2016-10-10T01:19:14Z","body":"Thank you, Jan!!"}}
{"actor":{"display_login":"khushroo-mistry"}}