pg-promise 认为我的数据库 table 不存在
pg-promise thinks my database table doesn't exist
我正在尝试起床 运行 pg-promise,但它认为我的数据库 table 不存在,我不确定为什么。
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)
.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,它显示 table 存在一条记录,并且 SQL 查询在那里工作。
public.test
也不起作用,calibration-training.public.test
或 Test
或 "Test"
或 "test"
.
也不起作用
psql
也说明table存在:
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)
那是因为 test
和 "Test"
是两个不同的 table 名字。
检查驼峰式大小写 + 转义 SQL Postgre 中的名称SQL ;)
我正在尝试起床 运行 pg-promise,但它认为我的数据库 table 不存在,我不确定为什么。
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)
.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,它显示 table 存在一条记录,并且 SQL 查询在那里工作。
public.test
也不起作用,calibration-training.public.test
或 Test
或 "Test"
或 "test"
.
psql
也说明table存在:
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)
那是因为 test
和 "Test"
是两个不同的 table 名字。
检查驼峰式大小写 + 转义 SQL Postgre 中的名称SQL ;)