将多个表保存在多个表中

好吧,所以我有一个巨大的问题,我不知道如何解决。 我有我的标题以下设置:

var Title = sequelize.define('title', { id: DataTypes.INTEGER, name: DataTypes.STRING, organization_id: DataTypes.INTEGER }, { freezeTableName: true, instanceMethods: { retrieveAll: function (onSuccess, onError) { Title.findAll({order: 'Rand'}, {raw: true}) .ok(onSuccess).error(onError); }, retrieveById: function (quote_id, onSuccess, onError) { Title.find({where: {id: quote_id}}, {raw: true}) .success(onSuccess).error(onError); }, add: function (selectedCompetence,onSuccess, onError) { var title = {id: null, name: this.dataValues.name, organization_id: this.dataValues.organization_id}; Title.build(this.dataValues) .save().ok(onSuccess).error(onError); }, updateById: function (quote_id, onSuccess, onError) { var id = quote_id; var quotes = this.quotes; Title.update({quotes: quotes}, {where: {id: id}}) .success(onSuccess).error(onError); }, removeById: function (quote_id, onSuccess, onError) { Title.destroy({where: {id: quote_id}}).success(onSuccess).error(onError); } } } ), Title_has_competence = sequelize.define('title_has_competence', { id: DataTypes.INTEGER, title_id: DataTypes.INTEGER, competence_id: DataTypes.INTEGER, competence_level_id: DataTypes.STRING },{ freezeTableName: true, instanceMethods: { retrieveAll: function (onSuccess, onError) { Title_has_competence.findAll({order: 'Rand'}, {raw: true}) .ok(onSuccess).error(onError); }, retrieveById: function (quote_id, onSuccess, onError) { Title_has_competence.find({where: {id: quote_id}}, {raw: true}) .success(onSuccess).error(onError); }, add: function (competence,onSuccess, onError) { var ins = {id: null, title_id:competence.title_id, competence_id: competence.id, competence_level_id: competence.level}; Title_has_competence.build(ins) .save().ok(onSuccess).error(onError); }, updateById: function (quote_id, onSuccess, onError) { var id = quote_id; var quotes = this.quotes; Title_has_competence.update({quotes: quotes}, {where: {id: id}}) .success(onSuccess).error(onError); }, removeById: function (quote_id, onSuccess, onError) { Title.destroy({where: {id: quote_id}}).success(onSuccess).error(onError); } } }); Title.belongsToMany(Title_has_competence,{foreignKey: 'competence_id'}); 

在我的系统中,当你创build一个标题时,你传递了一个权限数组:

在这种情况下,能力是数组: selectedCompetence一旦标题已经创build,我需要遍历selectedCompetence数组,并将其插入到Title_has_competence表中。

我觉得我已经littryly尝试一切从Hooks到次要的黑客,但没有任何工作。

任何人都可以把我推向正确的方向?

我曾尝试过的事情:

  Title.build(this.dataValues) .save().done(function(err, success, selectedCompetence){ var i = 0; // here selectedCompetence is undefined }); 

  Title.build(this.dataValues) .save(); Title.hook('afterCreate', function(success,selectedCompetence){ var i = 0; // here selectedCompetence is an object with other values }) 

  Title.create({id: null, name: this.dataValues.name, organization_id:this.dataValues.organization_id}).then(function(title, selectedCompetence) { var i = 0; // once again selectedCompetence is undefined }); 

我认为最接近的尝试是then()方法。 但是,在create()之后传递给then函数的参数只是刚刚创build的对象,没有其他的东西,所以then(function(title, someCompetence) ...不起作用。

你可以尝试这样的事情:

 // Let's say you already know the competence IDs var competenceIds = [ 1, 2, 3 ] // First, create the title Title.create({ id: 1, name: 'Some Title' }) .then(function(createdTitle) { // Build an array of promises. Each element waits for one competence // to be associated with the title. var associationPromises = []; competences.forEach(function(competenceId) { // associate one competence with the title associationPromises.push(Title_has_competence.create({ title_id: createdTitle.id, competence_id: competenceId, competence_level_id: 123 // whatever else is on the join table }) }); // Wait for all the competences to be associated before continuing return Sequelize.Promise.all(associationPromises); }); 

这是一个非常低级的方法,当你有复杂的关系时(比如连接多个实体的连接表:标题,能力和级别),这个方法就派上用场了。

但通常Sequelize可以自动处理很多这个(请参阅关系文档 )。 例如,如果你只有标题能力的关系,你可能只需写:

 var Title = sequelize.define(...) var Competence = sequelize.define(...) Competence.belongsToMany(Title, {through: 'title_has_competence'}); Title.belongsToMany(Competence, {through: 'title_has_competence'}); Title.create({name: 'Some Title'}).then(function(createdTitle) { return createdTitle.setCompetences([1, 2, 3]); }); 

(注:我其实也认为Sequelize 也可以处理你的情况,但是我还没有使用这个function;在上面的文档中search“连接表中的附加属性”)