I'm trying to create a temporary table, then execute a SELECT
query on it using the node-pg library. Using promises and .then()
, I wait for the table to be created (or it seems that should be the case), then do my selection query:
const tempText = `
CREATE TEMPORARY TABLE IF NOT EXISTS temp1 AS
(
SELECT colA, colB
FROM foo
GROUP BY colA, colB
)
`;
const queryText = `
SELECT colA, COUNT(colA) as total
FROM temp1
GROUP BY colA
ORDER BY COUNT(colA) DESC
`;
// using pooling
return db.query({ text: tempText, values })
.then(() => {
return db.query(queryText).then(({ rows }) => rows);
})
.catch((err) => { throw err; });
This code is all lives inside a GraphQL resolver. The trouble is, the first time I hit it via an API, I get "relation \"temp1\" does not exist", But all subsequent calls are successful and return data. Why would this be? I'm waiting for the first query to finish before doing the SELECT