当我像其他人一样设置该表的时间戳timestamp without time zone NOT NULL
时创建此表时,出现如下错误消息,但是当我尝试向该表中插入2015-08-16 18:51:05
时,却出现了错误,但其他表仍在工作,为什么以及如何解决呢?
CREATE TABLE IF NOT EXISTS "UserForgetPasswordPending"(
"UserForgetPasswordPendingId" SERIAL NOT NULL,
"Email" varchar(50) NOT NULL,
"TokenTimestamp" timestamp without time zone NOT NULL,
"Token" varchar(100) NOT NULL,
PRIMARY KEY ("UserForgetPasswordPendingId")
);
ALTER TABLE "UserForgetPasswordPending"
OWNER TO db_admin;
错误
[error: column "TokenTimestamp" is of type timestamp without time zone but expression is of type integer]
name: 'error',
length: 216,
severity: 'ERROR',
code: '42804',
detail: undefined,
hint: 'You will need to rewrite or cast the expression.',
插
var utc = moment(new Date()).unix();
var tokenTimestamp = moment.unix(utc).format('YYYY-MM-DD HH:mm:ss');
var upsertUserForgetPasswordPending = function(userEmail, tokenTimestamp, token) {
return new Promise(function (fulfill, reject){
var queryInsert = 'INSERT INTO "UserForgetPasswordPending" ("Email","TokenTimestamp","Token") SELECT $1,2,$3';
var queryUpsert = 'UPDATE "UserForgetPasswordPending" SET "TokenTimestamp" = $2, "Token" = $3 WHERE "Email" = $1';
var query = 'WITH upsert AS ('+queryUpsert+' RETURNING *) '+queryInsert+' WHERE NOT EXISTS (SELECT * FROM upsert)';
console.log(tokenTimestamp);
dbClient.query(query, [userEmail,tokenTimestamp,token], function(error, result) {
if (error) {
reject(error);
} else {
fulfill(result);
}
});
});
};
最佳答案
这是因为您要将整数数据插入到time stamp
列中。
更正以下语法:var queryInsert = 'INSERT INTO "UserForgetPasswordPending ("Email","TokenTimestamp","Token") SELECT $1,2,$3';
在上面的查询中,您选择2
作为TokenTimestamp
,这就是您收到此错误的原因。
您应该将2
替换为某些日期时间格式yyyy-mm-dd hh:mm:ss
。
例如:'2015-08-07 05:00:01'
关于javascript - 列的类型为时间戳,没有时区,但表达式的类型为整数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32034274/