gpt4 book ai didi

node.js - PostgreSQL/Node : Query Result Empty

转载 作者:行者123 更新时间:2023-11-29 14:32:39 25 4
gpt4 key购买 nike

我有一个返回 0 行的查询,但使用 pgadmin 或 dbeaver 执行相同的查询会返回一个包含行的结果集。

我注意到了这一点,因为我有一个应该返回行但没有返回行的 postgresql 函数。之后我开始调试。

其他查询不受影响。我尝试使用 knexjs (knex.raw()) 和 pg (client.query())。

因为,我使用不同的查询和读取连接字符串检查了十几次连接。

这真的很奇怪。

这里的重点是,为什么这在 dbeaver 中有效而不是在我的代码中。这是驱动程序的事吗?

查询

select id from (
select id, started_at from queue
where finished_at is null and started_at is not null order by id
) d
where date_part('minute',age(now(), started_at)) >= 5

我反复尝试,发现以下查询确实有效。

select id from queue 
where date_part('minute',age(now(), started_at)) >= 5;

select id from (
select id, started_at from queue
where finished_at is null and started_at is not null order by id
) d;

更新

不工作

const test = await this.knexInstance.raw(`
select id from (
select id, started_at from queue
where finished_at is null and started_at is not null order by id
) d
where date_part('minute',age(now(), started_at)) >= 5
`);
console.log(test.rows); // => []
console.log(test.rows.length); // => 0

工作

const test = await this.knexInstance.raw(`
select id from queue
where date_part('minute',age(now(), started_at)) >= 5;
`);
console.log(test.rows); // => Array(48083) [Object, Object, Object, Object, Object, Object, Object, Object, …]
console.log(test.rows.length); // => 48083

最佳答案

好的,我尝试重现这个,但得到了预期的结果。我正在使用 knex@0.14.4

const knex = require('knex')(config)

async function main () {
await knex.raw('create table queue ( id bigserial primary key, started_at timestamp with time zone not null default current_timestamp, finished_at timestamp with time zone);')
await knex('queue').insert({ started_at: knex.raw('now() - \'10 minutes\'::interval'), finished_at: null })
await knex('queue').insert({ started_at: knex.raw('now() - \'11 minutes\'::interval'), finished_at: null })
await knex('queue').insert({ started_at: knex.raw('now() - \'12 minutes\'::interval'), finished_at: null })
await knex('queue').insert({ started_at: knex.raw('now() - \'13 minutes\'::interval'), finished_at: null })

await knex('queue').insert({ started_at: knex.raw('now() - \'4 minutes\'::interval'), finished_at: null })
const test = await knex.raw(`
select id from (
select id, started_at from queue
where finished_at is null and started_at is not null order by id
) d
where date_part('minute',age(now(), started_at)) >= 5
`);
console.log(test.rows) // Array(4)
console.log(test.rows.length) // => 4
await knex.raw('drop table queue;')
await knex.destroy()
}

main()

我所能推荐的就是尝试在本地运行这个示例并观察结果。并尝试将 knex 升级到最新版本(如果不是)。

关于node.js - PostgreSQL/Node : Query Result Empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49295661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com