【发布时间】:2018-04-12 12:47:17
【问题描述】:
我有一个 sequelize 原始查询来从数据库中选择一些值。但是在 postgres 中运行查询时,即由 sequelize 生成的查询给出 2 行,在 sequelize 输出中,它只返回 1 行,返回的值是两行的混合.以下是我的代码。
const Sequelize = require('sequelize');
let sequelize;
sequelize = new Sequelize('db_oauth2','postgres', 'postgres',{ host: 'localhost',dialect: 'postgres'});
const invoices = await sequelize.query("SELECT invoice_amount, bill_period_from,to_char(bill_period_from,'Month') as month, bill_period_to FROM invoices WHERE invoices.user_id="+user_id+" and date_part('year', bill_period_from) = date_part('year', current_date) order by bill_period_from", { type: db.sequelize.QueryTypes.RAW});
console.log(invoices);
从控制台输出
Executing (default): SELECT invoice_amount, bill_period_from,to_char(bill_period_from,'Month') as month, bill_period_to FROM invoices WHERE invoices.user_id=112 and date_part('year', bill_period_from) = date_part('year', current_date) order by bill_period_from
[ [ anonymous {
invoice_amount: '120.000',
bill_period_from: 2018-01-31T18:30:00.000Z,
month: 'January ',
bill_period_to: 2018-02-27T18:30:00.000Z } ],
Result {
command: 'SELECT',
rowCount: 1,
oid: null,
rows: [ [Object] ],
fields: [ [Object], [Object], [Object], [Object] ],
_parsers:
[ [Function],
[Function: parseDate],
[Function: noParse],
[Function: parseDate] ],
RowCtor: [Function: anonymous],
rowAsArray: false,
_getTypeParser: [Function: bound ] } ]
我在 postgres 客户端执行了从控制台复制的相同查询,得到的结果如下
(invoice_amount;bill_period_from;month,bill_period_to)
300.000;"2018-01-01 00:00:00+05:30";"January ";"2018-01-31 00:00:00+05:30"
120.000;"2018-02-01 00:00:00+05:30";"February ";"2018-02-28 00:00:00+05:30"
所以在 sequelize 中,它只检索一行,即 postgres 输出中的第二行,而列月的值是 postgres 输出中的第一行。这是如何发生的并且是 sequelize 不是一个稳定的 ORM。 续集版本 4.26 Postgres 9.5
【问题讨论】:
标签: node.js sequelize.js sequelize-cli