好吧,我试图使用upsert
命令,但是我不断遇到错误,也不知道出了什么问题,我已经输入了想要的object
upsert
(即NewIssue
),它是基于其他调用的值。
功能
错误:(
{
"name": "SequelizeDatabaseError",
"parent": {
"fatal": false,
"errno": 1064,
"sqlState": "42000",
"code": "ER_PARSE_ERROR",
"sql": "INSERT INTO `ms_issue` VALUES () ON DUPLICATE KEY UPDATE ;"
},
"original": {
"fatal": false,
"errno": 1064,
"sqlState": "42000",
"code": "ER_PARSE_ERROR",
"sql": "INSERT INTO `ms_issue` VALUES () ON DUPLICATE KEY UPDATE ;"
},
"sql": "INSERT INTO `ms_issue` VALUES () ON DUPLICATE KEY UPDATE ;"
}
数据架构:
const Issue = sequelize.define('ms_issue', {
id_Issue: {
type: Sequelize.NUMBER,
primaryKey: true
},
id_IssueTag: {
type: Sequelize.NUMBER,
},
datetime_Issued: {
type: Sequelize.NOW
},
subject_Issue: {
type: Sequelize.STRING
},
desc_Issue: {
type: Sequelize.STRING
},
status_Issue: {
type: Sequelize.STRING
}
}, { timestamps: false, freezeTableName: true });
app.put('/issues/:id', (req, res) => {
const id_Staff = req.body.id_Staff
if (typeof id_Staff !== 'undefined' && typeof id_Staff === 'number') {
const id_Issue = parseInt(req.params.id)
if (typeof id_Issue !== 'undefined' && typeof id_Issue === 'number') {
Issue.findByPk(id_Issue)
.then(issue => {
if (issue) {
const newIssue = {
subject_Issue: req.body.subject || undefined,
desc_Issue: req.body.description || undefined,
id_IssueTag: req.body.tag || undefined
}
for (const obj in newIssue) {
if (typeof newIssue[obj] !== 'undefined') {
issue[obj] = newIssue[obj]
}
}
const NewIssue = issue
return NewIssue
} else res.status(404).send("Issue not found")
})
.then(NewIssue => {
return Issue.upsert(NewIssue)
.then(bool => {
if (bool === true) {
res.status(200).send("Issue has been updated")
res.status(200).send(NewIssue)
}
})
.catch(err => {
res.status(500).send(err)
})
})
.catch(err => {
console.log(err)
res.status(500).send("Cannot connect to database")
})
} else {
res.status(400).send("Invalid parameters: require 'id_Issue'")
}
} else {
res.status(401).send("Unauthorized access")
}
})
我想要的
能够根据要求在MariaDB
中插入/更新。 和说明:)
答案 0 :(得分:0)
生成的SQL无效-没有值:
"sql": "INSERT INTO `ms_issue` VALUES () ON DUPLICATE KEY UPDATE ;"
如果指定,则SQL应该有效,大致如下:
...
Issue.upsert({
id_Issue: id_Issue,
subject_Issue: NewIssue.subject_Issue,
id_IssueTag : NewIssue.id_IssueTag,
...