简体   繁体   English

如何更新sequelize.query中的多列

[英]How can I update multiple columns in sequelize.query

For now I update my table accounts with accountId and it's working perfectly. 现在,我用accountId更新我的表accounts ,它运行良好。

return models.sequelize.query("UPDATE \"accounts\" " +
                                        "SET \"accountId\" = "+newAccount.id+" " +
                                        "WHERE \"accountId\" = "+oldAccount.id+";").spread(function(){
            return 'success';
        });

What if I want to change not only accountId, but, say, the date. 如果我不仅要更改accountId,还要更改日期,该怎么办。 How should I write it then? 那我该怎么写呢? I've tried writing it with the comma 我试着用逗号写

"SET \\"accountId\\" = "+ newAccount.id+",\\"date\\" + newAccount.date + " WHERE...

but that doesn't seem to work. 但这似乎不起作用。

Appreciate your help. 感谢您的帮助。

UPDATE: in console I get this message [2015-10-25 16:42:00.909] [TRACE] main - Executing (default): UPDATE "accounts" SET "date" = Sun Oct 25 2015 16:42:00 GMT+0300 (MSK) WHERE "date" = Sun Oct 25 2015 16:41:53 GMT+0300 (MSK); 更新:在控制台中,我收到此消息[2015-10-25 16:42:00.909] [TRACE] main - Executing (default): UPDATE "accounts" SET "date" = Sun Oct 25 2015 16:42:00 GMT+0300 (MSK) WHERE "date" = Sun Oct 25 2015 16:41:53 GMT+0300 (MSK); but after that I don't get any 'success' message (data didn't change in db). 但是之后,我没有收到任何“成功”消息(db中的数据没有更改)。 May it happen because of data type? 是否可能因为数据类型而发生? I have 'timestamp with time zone' in my postgresql database. 我的PostgreSQL数据库中有“带时区的时间戳”。

I guess, here can be the same problem 我想, 这里可能是同样的问题

When you try to query by a date, you are sending over a JavaScript date object which gets converted into a string of the local time. 当您尝试按日期查询时,您正在发送JavaScript日期对象,该对象被转换为本地时间的字符串。 PostgreSQL then rejects this due to invalid syntax both because the date doesn't get quoted and because it won't recognize the format. PostgreSQL然后由于无效的语法而拒绝了它,这是因为日期没有被加上引号,并且因为它无法识别格式。

Whenever possible, try not use raw queries when using Sequelize, because Sequelize can do all of the necessary serialization and deserialization for you. 尽可能使用Sequelize时不要使用原始查询,因为Sequelize可以为您完成所有必要的序列化和反序列化。 Your some issue could easily be done by writing this: 您可以通过编写以下代码轻松解决一些问题:

var Account = sequelize.define('account', {
  accountId: Sequelize.INTEGER,
  date: Sequelize.DATE
});

Account.update({
  accountId: newAccount.id,
  date: new Date()
}, {
  where: {
    accountId: oldAccount.id
  }
}).then(function() {
  callback('success');
});

If you really want to do this with a raw query, you should convert your date object into something that PostgreSQL can read. 如果您真的想使用原始查询来执行此操作,则应将日期对象转换为PostgreSQL可以读取的内容。 You could do this with the moment library for instance: 例如,您可以使用时刻库​​来执行此操作:

var moment = require('moment');

models.sequelize.query("UPDATE \"accounts\" " +
 "SET \"accountId\" = " + newAccount.id + ", " +
   "\"date\" = '" + moment().format('YYYY-MM-DD HH:mm:ss') + "' " +
 "WHERE \"accountId\" = " + oldAccount.id + ";").spread(function(){
 return 'success';
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM