javascript - Sequelize associations: set[Models] adds new models instead of associating existing ones -
i'm using sequelize , i'm trying create associations between 2 different tables, x.belongsto(y)
, y.hasmany(x)
. after having done x.sety(yinstance)
, y.getxs()
seems new rows have been added x , no associations created instances have been created.
var promise = require("bluebird"), sequelize = require("sequelize"); var sequelize = new sequelize("test", "postgres", "password", { host: "localhost", dialect: "postgres", pool: { max: 5, min: 0, idle: 10000 } }); var schedule = sequelize.define("schedule", { website: { type: sequelize.string } }); var siteconfig = sequelize.define("siteconfig", { systemtype: { type: sequelize.string } }); var selector = sequelize.define("selector", { type: { type: sequelize.string }, content: { type: sequelize.string } }); selector.belongsto(siteconfig); siteconfig.hasmany(selector); var testschedule = { website: "google.com" }; var testsiteconfig = { systemtype: "one" }; var testselectors = [ {type: "foo", content: "foo"}, {type: "foo", content: "bar"} ]; promise.all([ schedule.sync({force: true}), siteconfig.sync({force: true}), selector.sync({force: true}) ]).then(function () { return promise.all([ schedule.create(testschedule), siteconfig.create(testsiteconfig), selector.bulkcreate(testselectors) ]); }).spread(function (schedule, siteconfig, selectors) { return promise.map(selectors, function (selector) { return selector.setsiteconfig(siteconfig); }).then(function (array) { return siteconfig.getselectors(); }).each(function (selector) { // expect "foo" , "bar" instead null console.log("selector content:", selector.get("content")); }); });
i'd expect code add siteconfigid
column selectors
siteconfig.getselectors()
return testselectors. how can achieve this?
[update]
it turns out had earlier wrong. method setsiteconfig()
not want use. checked db , looks sequelize created 2 new records instead of associating existing foo/bar selectors:
test=# select * "selectors"; id | type | content | createdat | updatedat | siteconfigid ----+------+---------+----------------------------+----------------------------+-------------- 1 | foo | foo | 2015-04-05 20:38:55.282-07 | 2015-04-05 20:38:55.282-07 | 2 | foo | bar | 2015-04-05 20:38:55.282-07 | 2015-04-05 20:38:55.282-07 | 3 | | | 2015-04-05 20:38:55.282-07 | 2015-04-05 20:38:55.311-07 | 1 4 | | | 2015-04-05 20:38:55.282-07 | 2015-04-05 20:38:55.31-07 | 1
so different? can't use setsiteconfig
on child rows, instead call addselectors
on siteconfig , pass in selectors want associate. see updated code below.
changed promise
variable bpromise
because node has native promise
module cause conflict. believe sequelize has bluebird built-in can use sequelize.promise
.
removed nested promise in spread
call because there no need it.
side note: promise.all
returns single result array don't think should using .spread()
.
var bpromise = require("bluebird"); var sequelize = require("sequelize"); var sequelize = new sequelize('test', 'root', 'password', { host: "localhost", dialect: "postgres", pool: { max: 5, min: 0, idle: 10000 } }); var schedule = sequelize.define("schedule", { website: { type: sequelize.string } }); var siteconfig = sequelize.define("siteconfig", { systemtype: { type: sequelize.string } }); var selector = sequelize.define("selector", { type: { type: sequelize.string }, content: { type: sequelize.string } }); selector.belongsto(siteconfig); siteconfig.hasmany(selector); var testschedule = { website: "google.com" }; var testsiteconfig = { systemtype: "one" }; var testselectors = [ {type: "foo", content: "foo"}, {type: "foo", content: "bar"} ]; sequelize.sync({ force: true }) .then(function(result) { return bpromise.all([ schedule.create(testschedule), siteconfig.create(testsiteconfig), selector.bulkcreate(testselectors, { returning: true }) ]); }) .then(function(result) { var siteconfig = result[1]; var selectors = result[2]; return siteconfig.addselectors(selectors); }) .then(function (result) { return this.siteconfig.getselectors(); }) .each(function(result) { console.log('boomshakalaka:', result.get()); }) .catch(function(error) { console.log(error); });
Comments
Post a Comment