我正在尝试使用node-postgres在我的postgres数据库中查询名为DOCUMENT
的模式。
我似乎无法让查询针对指定的架构运行。
此查询使用psql直接针对postgres运行正常
SELECT * FROM "DOCUMENT".document_metadata m
LEFT OUTER JOIN "DOCUMENT".document_attributes a
ON a.document_id = m.id
此代码会在下面产生错误
const query = `SELECT * FROM "DOCUMENT".document_metadata m
LEFT OUTER JOIN "DOCUMENT".document_attributes a
ON a.document_id = m.id`
const metadata = await db.query(query)
错误
error: relation "DOCUMENT.document_metadata" does not exist
at Connection.parseE (/usr/src/app/node_modules/pg/lib/connection.js:602:11)
at Connection.parseMessage (/usr/src/app/node_modules/pg/lib/connection.js:399:19)
at Socket.<anonymous> (/usr/src/app/node_modules/pg/lib/connection.js:121:22)
at Socket.emit (events.js:189:13)
at addChunk (_stream_readable.js:284:12)
at readableAddChunk (_stream_readable.js:265:11)
at Socket.Readable.push (_stream_readable.js:220:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
我也尝试过
const query = `SET search_path TO 'DOCUMENT';
SELECT * FROM document_metadata m
LEFT OUTER JOIN document_attributes a
ON a.document_id = m.id;`
const metadata = await db.query(query)
产生错误
error: relation "document_metadata" does not exist
at Connection.parseE (/usr/src/app/node_modules/pg/lib/connection.js:602:11)
at Connection.parseMessage (/usr/src/app/node_modules/pg/lib/connection.js:399:19)
at Socket.<anonymous> (/usr/src/app/node_modules/pg/lib/connection.js:121:22)
at Socket.emit (events.js:189:13)
at addChunk (_stream_readable.js:284:12)
at readableAddChunk (_stream_readable.js:265:11)
at Socket.Readable.push (_stream_readable.js:220:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
更新 当我直接在psql中运行这些相同的查询时,我看到带有表名的10行。当我通过节点代码运行此命令时,我看不到任何行:
const metadata4 = await db.query('SHOW search_path;')
console.log('search_path after setting = ', metadata4.rows) // prints [ { search_path: '"DOCUMENT"' } ]
const tables = await db.query(`SELECT * FROM information_schema.tables where table_schema = 'DOCUMENT';`)
console.log('tables = ', tables.rows) // prints []
答案 0 :(得分:2)
也许是大写问题。 您是否尝试使用小写模式名称? 或在表名周围添加引号:
const query = 'SELECT * FROM "DOCUMENT"."document_metadata" m
LEFT OUTER JOIN "DOCUMENT"."document_attributes" a
ON a.document_id = m.id'
无论如何,如果您想在节点js中定义模式,而无需在不同模式之间切换,那么solution是一个不错的选择。 默认架构可以来自环境变量。
const Pool = require('pg').Pool
const Client = require('pg').Client
class EnhancedClient extends Client {
getStartupConf() {
if (process.env.PG_OPTIONS) {
try {
const options = JSON.parse(process.env.PG_OPTIONS);
return {
...super.getStartupConf(),
...options,
};
} catch (e) {
console.error(e);
// Coalesce to super.getStartupConf() on parse error
}
}
return super.getStartupConf();
}
}
const pool = new Pool({
Client: EnhancedClient,
user: 'postgres',
host: 'localhost',
database: 'postgres',
password: 'postgres',
port: 5432
})
在此示例中,PG_OPTIONS是字符串化的JSON,例如通过命令行,PM2生态系统文件等提供。
例如:PG_OPTIONS='{"search_path":"DOCUMENT"}' node app.js
我从Windows命令提示符开始(对此很抱歉:))
SET PG_OPTIONS={"search_path":"DOCUMENT"}
node app.js
答案 1 :(得分:0)
只需在单独的行中执行:
$(document).on('click', '#printBtn', function () {
window.print();
});
DOCUMENT是您的架构名称,然后执行常规查询:
db.query("SET search_path TO 'DOCUMENT';")