답변:
Sequelize 버전 3 이상을 사용하는 사람은 다음을 사용하십시오.
Model.destroy({
where: {
// criteria
}
})
다음 파일에서 단계별로 코드를 자세히 검색했습니다.
https://github.com/sdepold/sequelize/blob/master/test/Model/destroy.js
https://github.com/sdepold/sequelize/blob/master/lib/model.js#L140
https://github.com/sdepold/sequelize/blob/master/lib/query-interface.js#L207-217
https://github.com/sdepold/sequelize/blob/master/lib/connectors/mysql/query-generator.js
내가 찾은 것 :
deleteAll 메서드가없고 레코드에서 호출 할 수있는 destroy () 메서드가 있습니다. 예를 들면 다음과 같습니다.
Project.find(123).on('success', function(project) {
project.destroy().on('success', function(u) {
if (u && u.deletedAt) {
// successfully deleted the project
}
})
})
질문이 여전히 관련이 있는지 모르겠지만 Sequelize의 문서에서 다음을 발견했습니다.
User.destroy('`name` LIKE "J%"').success(function() {
// We just deleted all rows that have a name starting with "J"
})
http://sequelizejs.com/blog/state-of-v1-7-0
도움이 되었기를 바랍니다.
이 예제는 콜백 대신 약속하는 방법을 보여줍니다.
Model.destroy({
where: {
id: 123 //this will be your id that you want to delete
}
}).then(function(rowDeleted){ // rowDeleted will return number of rows deleted
if(rowDeleted === 1){
console.log('Deleted successfully');
}
}, function(err){
console.log(err);
});
자세한 정보는이 링크를 확인하십시오. http://docs.sequelizejs.com/en/latest/api/model/#destroyoptions-promiseinteger
새 버전에서는 다음과 같이 시도 할 수 있습니다.
function (req,res) {
model.destroy({
where: {
id: req.params.id
}
})
.then(function (deletedRecord) {
if(deletedRecord === 1){
res.status(200).json({message:"Deleted successfully"});
}
else
{
res.status(404).json({message:"record not found"})
}
})
.catch(function (error){
res.status(500).json(error);
});
시간을 절약 할 수 있도록 Sails에 대해 다음과 같이 작성했습니다.
사용 예 :
// Delete the user with id=4
User.findAndDelete(4,function(error,result){
// all done
});
// Delete all users with type === 'suspended'
User.findAndDelete({
type: 'suspended'
},function(error,result){
// all done
});
출처:
/**
* Retrieve models which match `where`, then delete them
*/
function findAndDelete (where,callback) {
// Handle *where* argument which is specified as an integer
if (_.isFinite(+where)) {
where = {
id: where
};
}
Model.findAll({
where:where
}).success(function(collection) {
if (collection) {
if (_.isArray(collection)) {
Model.deleteAll(collection, callback);
}
else {
collection.destroy().
success(_.unprefix(callback)).
error(callback);
}
}
else {
callback(null,collection);
}
}).error(callback);
}
/**
* Delete all `models` using the query chainer
*/
deleteAll: function (models) {
var chainer = new Sequelize.Utils.QueryChainer();
_.each(models,function(m,index) {
chainer.add(m.destroy());
});
return chainer.run();
}
도움이 되었기를 바랍니다.
const StudentSequelize = require("../models/studientSequelize"); const StudentWork = StudentSequelize.Student; const id = req.params.id; StudentWork.findByPk(id) // here i fetch result by ID sequelize V. 5 .then( resultToDelete=>{ resultToDelete.destroy(id); // when i find the result i deleted it by destroy function }) .then( resultAfterDestroy=>{ console.log("Deleted :",resultAfterDestroy); }) .catch(err=> console.log(err));