[英]Sequelize.js - Persist model with association
I'm trying to save an entity with the following models and save method but it only creates the parent entity. 我正在尝试使用以下模型和保存方法保存实体,但它只会创建父实体。 See:
看到:
var Project = database.define('projects', {
name : {
type : Sequelize.STRING,
allowNull : false,
unique : true
},
archived : {
type : Sequelize.BOOLEAN,
allowNull : false,
defaultValue : false
}
});
var Sprint = database.define('sprints', {
name : {
type : Sequelize.STRING,
allowNull : false,
unique : true
},
start : {
type : Sequelize.DATE
},
end : {
type : Sequelize.DATE
}
});
Project.hasMany(Sprint, {
as : 'sprints'
});
Sprint.belongsTo(Project);
The save method in Express: Express中的保存方法:
var project = req.body;
models.Project.create(project).then(function(project){
return res.json(project);
}, function(err) {
return next(err);
});
The req.body is something like: req.body类似于:
{
archived : false,
name : "Projeto 1",
sprints : [{
end : "2016-02-25T03:00:00.000Z",
name : "Sprint 1",
start : "2016-02-19T02:00:00.000Z"
}
]
}
The result is one record of Project created in database. 结果是在数据库中创建的Project的一条记录。 Nothing more.
而已。
Sprint entry is also new, I was expecting it could be created together with Project. Sprint条目也是新的,我希望它可以与Project一起创建。
Any ideas? 有任何想法吗?
Thanks 谢谢
Ok, now it saves Project and it's Sprints: 好的,现在它保存了Project和Sprint:
var project = req.body;
models.Project.create(project, {
include : [{
model : models.Sprint,
as : 'sprints'
}
]
}).then(function (project) {
return res.json(project);
}, function (err) {
return next(err);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.