Node.js 节点js和Sequelize插入数据库
在我的项目中,我使用MySQL数据库和Sequelize Js, 我创建了两个模型: 邮政编码模式:Node.js 节点js和Sequelize插入数据库,node.js,express,model,insert,sequelize.js,Node.js,Express,Model,Insert,Sequelize.js,在我的项目中,我使用MySQL数据库和Sequelize Js, 我创建了两个模型: 邮政编码模式: module.exports = function(sequelize, Sequelize) { var Post_code = sequelize.define('post_code', { id: { autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER(11
module.exports = function(sequelize, Sequelize) {
var Post_code = sequelize.define('post_code', {
id: {
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER(11)
},
code: {
type: Sequelize.STRING(16),
allowNull: false,
unique: true
},
city: {
type: Sequelize.STRING(45),
allowNull: false
}
},{
freezeTableName: true,
underscored: true
});
Post_code.associate = function(models) {
models.post_code.hasMany(models.building);
};
return Post_code;
}
建筑模型:
module.exports = function(sequelize, Sequelize) {
var Building = sequelize.define('building', {
id: {
autoIncrement: true,
primaryKey: true,
allowNull: false,
type: Sequelize.INTEGER(11)
},
name: {
type: Sequelize.STRING(100),
allowNull: false
},
number: {
type: Sequelize.INTEGER(11),
allowNull: false
},
address: {
type: Sequelize.STRING(255),
allowNull: false
},
latitude: {
type: Sequelize.DECIMAL(9,6),
allowNull: false
},
longitude: {
type: Sequelize.DECIMAL(9,6),
allowNull: false
}
},{
freezeTableName: true,
underscored: true
});
Building.associate = function (models) {
models.building.belongsTo(models.post_code, {
foreignKey: {
allowNull: false
}
});
models.building.hasMany(models.flat);
};
return Building;
};
它们是一对多的关系,因此邮政编码有许多建筑物:
当POST请求发送到此路由时,我想将新建筑添加到数据库:
"/post-codes/:post_code_id/buildings"
我可以访问post_code_id,但我不知道如何将post_code模型与建筑正确关联
我试着这样做:
models.post_code.findById(req.params.post_code_id)
.then(function(postCode){
postCode.setBuilding(
models.building.create({
}).then(function(building){});
);
});
db.post_code.find({
where: {
id: req.params.post_code_id
}
}).then(post_code => {
if (!post_code) {
return res.status(404).send({
message: 'No post_code with that identifier has been found'
});
} else {
//create here your building
}
}).catch(err => {
return res.jsonp(err)
});
但没有结果。如果有人能解释如何正确插入,我将不胜感激。我还没有听说过“创建”上的“包含”选项,但您可以这样做:
models.post_code.findById(req.params.post_code_id)
.then(function(postCode){
postCode.setBuilding(
models.building.create({
}).then(function(building){});
);
});
db.post_code.find({
where: {
id: req.params.post_code_id
}
}).then(post_code => {
if (!post_code) {
return res.status(404).send({
message: 'No post_code with that identifier has been found'
});
} else {
//create here your building
}
}).catch(err => {
return res.jsonp(err)
});
我不知道这是否是最好的方法,但它首先验证post_代码是否存在。您是否只需要添加建筑,然后从您收到的参数将post codeId添加到对象?@Ellebkey是的,这是正确的,但我正在Sequelize中搜索一些生成关联的内置方法。另一件事是,是的,我可以将post_code_id直接添加到我将要创建的建筑中,但是如果发生这样的情况,这个参数将是数据库中不存在的post_code的“id”,我将得到一个错误。我并没有找到简单的解释,但model.create中有一个“include”选项,我可以使用它以更安全的方式创建关联。这里是关于这个选项的文档链接,如果你想看的话,也许你可以看看。无论如何,谢谢你的回复。这就是定义N:M关系的方式,在你的情况下,你只需要1:N关系。include是在需要进行连接时使用它。所以,当你在建筑物上使用find时,你会包含DePaseType代码,但是如果你要查看“HasMany或归属关系”的创建元素的部分,我的例子考虑BelongsToMany,因为构建属于许多PASSY代码,并且他们正在提出用许多标签创建/关联产品的方法,然后,他们正在将tags:key设置为标记/对象的数组