【问题标题】:CockroachDB with node js pg driverCockroachDB 与节点 js pg 驱动程序
【发布时间】:2020-12-11 09:19:10
【问题描述】:

我正在尝试使用 pg 驱动程序连接 CockroachDB 和 Nodes JS。我能够成功建立连接,但是每次查询表时:它仅在我为表名加上数据库名称前缀时才有效,否则它会引发关系不存在错误。虽然我在建立数据库连接时指定了数据库名称。

我用来建立数据库连接的代码:

    var pg = require('pg');
    var config = {
        user: 'root',
        host: 'localhost',
        database: 'testDB',
        port: 26257
    };
    var pool = new pg.Pool(config);   
    const client = await pool.connect();

执行此行工作正常,因为我在表名前加上 DBname:

const response = await client.query('select * from testDB.test');

执行此行会引发以下错误:

const response = await client.query('select * from test');
(node:12797) UnhandledPromiseRejectionWarning: error: relation "test" does not exist
    at Parser.parseErrorMessage (/Users/naveenkumar/Ucars/node-cockroachDB/node_modules/pg-protocol/dist/parser.js:278:15)
    at Parser.handlePacket (/Users/naveenkumar/Ucars/node-cockroachDB/node_modules/pg-protocol/dist/parser.js:126:29)
    at Parser.parse (/Users/naveenkumar/Ucars/node-cockroachDB/node_modules/pg-protocol/dist/parser.js:39:38)
    at Socket.<anonymous> (/Users/naveenkumar/Ucars/node-cockroachDB/node_modules/pg-protocol/dist/index.js:10:42)
    at Socket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:304:12)
    at readableAddChunk (_stream_readable.js:280:9)
    at Socket.Readable.push (_stream_readable.js:219:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
    at TCP.callbackTrampoline (internal/async_hooks.js:123:14)
(Use `node --trace-warnings ...` to show where the warning was created)
<node_internals>/internal/process/warning.js:33
(node:12797) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
<node_internals>/internal/process/warning.js:33
(node:12797) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

感谢任何形式的帮助,在此先感谢 :)

【问题讨论】:

    标签: node.js orm pg cockroachdb


    【解决方案1】:

    编辑:数据库名称应全部小写。见https://www.cockroachlabs.com/docs/stable/keywords-and-identifiers.html#rules-for-identifiers

    defaultdb> CREATE DATABASE TeSt;
    CREATE DATABASE
    
    Time: 166.382ms
    
    defaultdb> SHOW DATABASES;
            database_name        |       owner
    -----------------------------+---------------------
      defaultdb                  | root
      test                       | lauren
      testdb                     | lauren
    

    使用 CockroachDB v20.2.2 和 pg v8.5.1,我无法重现该问题。以下按预期工作:

    const { Pool } = require("pg");
    
    const config = {
      ...
      database: "testdb",
      ...
    };
    
    const pool = new Pool(config);
    ;(async function() {
      const client = await pool.connect();
      await client.query("select * from test_table", (err, res) => {
        console.log(err, res);
        client.end();
      });
    })()  
    
    

    【讨论】:

    • 谢谢!!,它成功了,数据库名称中的大写是问题。
    猜你喜欢
    • 1970-01-01
    • 2011-02-23
    • 2017-11-01
    • 2016-03-13
    • 2017-12-15
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多