【发布时间】:2016-02-07 17:34:07
【问题描述】:
我搜索了任何现有的模式,但没有找到任何东西。
所以,问题是我想在我的 MySQL 数据库中存储一些 key 值,这将是 唯一。我像这样在服务器端生成它:
var pc = require('password-creator');
var key = pc.create(20); // "f9Wp>=\qJqx]rwwbbiQ8
然后我将它发送到数据库:
connection.query(db_query, function (err, rows, fields) {
if(err){
if(err.code == "ER_DUP_ENTRY"){
// here I could attempt again with newly generated key by
// calling another query to database but this is giving me
// a limited amount of such attempts
}
}
else{
// there was no duplicate
}
});
所以一切看起来都很好,但获得ER_DUP_ENTRY 的可能性很小。现在是我的问题:
如何以正确的方式解决这种情况?我的意思是我总是想在数据库中输入一些key 值。我想到了循环,但这感觉很愚蠢,因为它是对数据库的异步查询。在这种情况下,我基本上是在寻找正确的方法。
【问题讨论】:
标签: javascript mysql node.js unique-key