1

我正在使用 React、Node、Express、Massive 和 PostgreSQL 开发一个 Web 应用程序,并且在执行一个特定查询时遇到了问题:

SELECT 
  COUNT(DISTINCT cu.user_id) AS ucount, 
  COUNT(DISTINCT p.project_id) AS pcount, 
  COUNT(DISTINCT t.task_id) AS tcount, 
  c.clique_id, c.clique_name, c.admin_id, c.created_on 
FROM cliques c 
FULL OUTER JOIN cliques_users cu ON cu.clique_id = c.clique_id 
FULL OUTER JOIN users u ON u.user_id = cu.user_id 
FULL OUTER JOIN projects p ON p.clique_id = cu.clique_id 
FULL OUTER JOIN tasks t ON t.clique_id = cu.clique_id 
WHERE c.clique_id IN ( 
  SELECT cu.clique_id FROM cliques_users cu WHERE cu.user_id = 3 
) 
GROUP BY c.clique_id;

**注意:我在子查询上使用 3 只是为了测试。

我正在使用 Postico 测试我的 SQL 语句,这个查询返回了我期望的结果。但是,当在应用程序本身上完成此操作时,通过访问端点来请求数据,服务器会抛出错误:

{ error: function     count(integer) does not exist at ...
name: 'error',
length: 225,
severity: 'ERROR',
code: '42883',
detail: undefined,
hint: 'No function matches the given name and argument types. You might need to add explicit type casts.',
position: '55',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_func.c',
line: '528',
routine: 'ParseFuncOrColumn' }

命中端点时运行的回调函数如下所示:

(req, res, next) => {
  req.app.get( 'db' )
    .clique.getCliqueSummaryQuery( req.params.user_id )
    .then( response => {
      res.status(200).json(response);
    })
    .catch( err => {
      console.log( 'getMyCliquesInfo failed: ', err );
      res.status(500).json( err );
    });
}

getCliqueSummaryQuery() 运行查询,将 url 参数作为变量传递,该变量将替换我为测试而硬编码的 3。变量值和硬编码值都会发生错误。我已将查询直接从 Postico 复制到我的 sql 文件中。

任何人都知道为什么它以一种方式工作而不是另一种?

4

0 回答 0