续集 upsert 布尔值不起作用
Sequelize upsert boolean not working
我正在尝试在 Sequelize 中使用 upsert 方法,但布尔值不一致 returns。 updatedData 参数似乎始终为 false,尽管数据库中的数据已更新。这是我的代码:
router.post('/deploy', function (req, res) {
var data;
data = req.body;
//Update data in mySQL
db.site.upsert({
name: data.name,
templateId: data.templateId,
siteName: data.siteName,
domain: utils.formatDomain(data.domain),
mainEmail: data.mainEmail,
mainPhone: utils.removePhoneNumberFormat(data.mainPhone),
mainExt: ''
}, {
where: {
domain: data.domain
},
}).then(function (updatedData) {
console.log(updatedData);
if (updatedData) {
console.log('New data written successfully');
return res.status(200).send('New data written');
} else {
console.log('Data did not change from the origin');
return res.status(200).send('Nothing changed');
}
}).catch(function (e) {
console.log('Deploy Controller Error: s% ', e);
return res.status(500).end('unexpected_error');
});
});
我确定我在这里做错了什么,但我们将不胜感激任何帮助或反馈。谢谢,我希望你今天过得愉快。 :)
确定 - 这是三个解决方法,HTH :)
解决方法 1:使用两个 Sequelize 调用
您可以使用 findById
(或 count
),而不是使用 upsert
,然后根据第一个调用执行 insert
或 update
returns。这不会像 upsert 那样快,但根据您的性能需求,它可能没问题。部分代码:
find_mutate: function find_mutate(id, data) {
return models.site.findById(id)
.then(function insert_or_update_instance(instance) {
if(instance !== null) {
return instance.update(data);
}
else {
return models.site.create(data);
}
});
},
count_mutate: function upsert_using_count_mutate(id, data) {
return models.site.count({ where: { domain: id } })
.then(function insert_or_update_instance(count) {
if(count !== 0) {
return models.site.update(data, { where: { domain: id } });
}
else {
return models.site.create(data);
}
});
}
请参阅 here 以获取对此进行演示的测试脚本。
解决方法 2:手动构建原始查询
我没有将其包含在刚刚链接到的 github 示例中,但您也可以使用 Sequelize.query
手动构建和执行更新插入查询。我不推荐这个:
- SQL injection attacks
的潜在漏洞
- 生成的代码很难维护和错误检查
就是说,这是 MySQL:
的示例查询
INSERT INTO `site` (`name`,`domain`,`sq_created_at`,`sq_updated_at`) VALUES ('name1','instance1','2015-11-20 13:24:50','2015-11-20 13:24:50') ON DUPLICATE KEY UPDATE `name`=VALUES(`name`), `domain`=VALUES(`domain`), `sq_updated_at`=VALUES(`sq_updated_at`);
解决方法 3:更多调试
在 3 种解决方法中,找出并解决根本问题可能是您的最佳举措。 upsert
作为书面作品*,因此您报告的行为实际上可能是由代码中其他地方的另一个错误引起的。
*警告:可能无关:#4885
这方面的一些建议,对您来说可能是多余的或不是新的:
node-debug
是一个很棒的工具。
- 记录 SQL 并检查
rowCount
的值也可能有用
注意:更新插入和 where
在您的代码示例中,您指定了 where 条件。 upsert
根据对象的主键构建自己的 where 子句,因此不需要此选项键。
我正在尝试在 Sequelize 中使用 upsert 方法,但布尔值不一致 returns。 updatedData 参数似乎始终为 false,尽管数据库中的数据已更新。这是我的代码:
router.post('/deploy', function (req, res) {
var data;
data = req.body;
//Update data in mySQL
db.site.upsert({
name: data.name,
templateId: data.templateId,
siteName: data.siteName,
domain: utils.formatDomain(data.domain),
mainEmail: data.mainEmail,
mainPhone: utils.removePhoneNumberFormat(data.mainPhone),
mainExt: ''
}, {
where: {
domain: data.domain
},
}).then(function (updatedData) {
console.log(updatedData);
if (updatedData) {
console.log('New data written successfully');
return res.status(200).send('New data written');
} else {
console.log('Data did not change from the origin');
return res.status(200).send('Nothing changed');
}
}).catch(function (e) {
console.log('Deploy Controller Error: s% ', e);
return res.status(500).end('unexpected_error');
});
});
我确定我在这里做错了什么,但我们将不胜感激任何帮助或反馈。谢谢,我希望你今天过得愉快。 :)
确定 - 这是三个解决方法,HTH :)
解决方法 1:使用两个 Sequelize 调用
您可以使用 findById
(或 count
),而不是使用 upsert
,然后根据第一个调用执行 insert
或 update
returns。这不会像 upsert 那样快,但根据您的性能需求,它可能没问题。部分代码:
find_mutate: function find_mutate(id, data) {
return models.site.findById(id)
.then(function insert_or_update_instance(instance) {
if(instance !== null) {
return instance.update(data);
}
else {
return models.site.create(data);
}
});
},
count_mutate: function upsert_using_count_mutate(id, data) {
return models.site.count({ where: { domain: id } })
.then(function insert_or_update_instance(count) {
if(count !== 0) {
return models.site.update(data, { where: { domain: id } });
}
else {
return models.site.create(data);
}
});
}
请参阅 here 以获取对此进行演示的测试脚本。
解决方法 2:手动构建原始查询
我没有将其包含在刚刚链接到的 github 示例中,但您也可以使用 Sequelize.query
手动构建和执行更新插入查询。我不推荐这个:
- SQL injection attacks 的潜在漏洞
- 生成的代码很难维护和错误检查
就是说,这是 MySQL:
的示例查询INSERT INTO `site` (`name`,`domain`,`sq_created_at`,`sq_updated_at`) VALUES ('name1','instance1','2015-11-20 13:24:50','2015-11-20 13:24:50') ON DUPLICATE KEY UPDATE `name`=VALUES(`name`), `domain`=VALUES(`domain`), `sq_updated_at`=VALUES(`sq_updated_at`);
解决方法 3:更多调试
在 3 种解决方法中,找出并解决根本问题可能是您的最佳举措。 upsert
作为书面作品*,因此您报告的行为实际上可能是由代码中其他地方的另一个错误引起的。
*警告:可能无关:#4885
这方面的一些建议,对您来说可能是多余的或不是新的:
node-debug
是一个很棒的工具。- 记录 SQL 并检查
rowCount
的值也可能有用
注意:更新插入和 where
在您的代码示例中,您指定了 where 条件。 upsert
根据对象的主键构建自己的 where 子句,因此不需要此选项键。