pg-promise 认为我的数据库表不存在

Posted

技术标签:

【中文标题】pg-promise 认为我的数据库表不存在【英文标题】:pg-promise thinks my database table doesn't exist 【发布时间】:2021-12-08 17:46:50 【问题描述】:

我正在尝试使用pg-promise 启动并运行,但它认为我的数据库表不存在,我不知道为什么。

app.js

require("dotenv").config();
const express = require("express");
const  db  = require("./database.js");
const app = express();
const PORT = 8080;

app.get("/", (req, res) => 
  db.one("SELECT * FROM test WHERE id = $1", 1)
    .then((record) => 
      console.log(record);
      res.send("Success");
    )
    .catch((error) => 
      console.log(error);
      res.send("Error");
    );
);

app.listen(PORT, () => 
  console.log(`Example app listening at http://localhost:$PORT`);
);

.env

DATABASE_CONNECTION=postgres://adamzerner:<iputmyrealpasswordhere>@localhost:5432/calibration-training

database.js

const pgp = require("pg-promise")();
const db = pgp(process.env.DATABASE_CONNECTION);

module.exports =  pgp, db ;

然后当我到达端点时,这就是记录的内容:

code/calibration-training-api [master●] » yarn start
yarn run v1.22.10
$ node app.js
Example app listening at http://localhost:8080
error: relation "test" does not exist
    at Parser.parseErrorMessage (/Users/adamzerner/code/calibration-training-api/node_modules/pg-protocol/dist/parser.js:287:98)
    at Parser.handlePacket (/Users/adamzerner/code/calibration-training-api/node_modules/pg-protocol/dist/parser.js:126:29)
    at Parser.parse (/Users/adamzerner/code/calibration-training-api/node_modules/pg-protocol/dist/parser.js:39:38)
    at Socket.<anonymous> (/Users/adamzerner/code/calibration-training-api/node_modules/pg-protocol/dist/index.js:11:42)
    at Socket.emit (node:events:390:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:199:23) 
  length: 103,
  severity: 'ERROR',
  code: '42P01',
  detail: undefined,
  hint: undefined,
  position: '15',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'parse_relation.c',
  line: '1384',
  routine: 'parserOpenTable'

但是查看Beekeeper Studio,它显示该表存在一条记录,并且SQL查询在那里工作。

public.test 也不起作用,calibration-training.public.testTest"Test""test" 也不起作用。

psql也表明该表存在:

code/calibration-training-api [master●] » psql
psql (14.0)
Type "help" for help.

adamzerner=# \c calibration-training
You are now connected to database "calibration-training" as user "adamzerner".
calibration-training=# \dt
         List of relations
 Schema | Name | Type  |   Owner
--------+------+-------+------------
 public | Test | table | adamzerner
(1 row)

【问题讨论】:

pg-promise没有实现任何AI,所以它无法思考,对不起(作者)。 【参考方案1】:

这是因为 test"Test" 是 2 个不同的表名。

在 PostgreSQL 中检查驼峰式 + 转义 SQL 名称 ;)

【讨论】:

哦,哇,我可以发誓我已经尝试过"Test"。感谢您的协助! (我是 postgres 的新手。)

以上是关于pg-promise 认为我的数据库表不存在的主要内容,如果未能解决你的问题,请参考以下文章

PHP - MySQL - 表不存在

更新后 LAMPP 错误 #1932:表不存在

Java Spring 表不存在

Laravel 模型 - 如果数据库表不存在则返回 null

根据android studio编译器,数据库表不存在

CREATE TABLE 后表不存在