PSQL 服务器,'relation does not exist' 通过 Postman 发送请求时
PSQL server, 'relation does not exist' when sending a request through Postman
我有一个非常基本的sql文件,代码:
\c postgres
DROP DATABASE IF EXISTS project1;
CREATE DATABASE project1;
\c parky
CREATE TABLE works (
id SERIAL PRIMARY KEY,
workshop TEXT NOT NULL,
attendee TEXT NOT NULL
);
这是我的 Javascript 服务器使用的:
require('dotenv').config()
const bodyParser = require("body-parser");
const express = require("express");
const app = express();
app.set("port", 8080);
app.use(bodyParser.json({ type: "application/json" }));
app.use(bodyParser.urlencoded({ extended: true }));
const Pool = require("pg").Pool;
const config = {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: "project1"
};
const pool = new Pool(config);
//Add an attendee to workshop
app.post("/api", async (req, res) => {
const ws = req.body.workshop;
const att = req.body.attendee;
try{
const template = "SELECT * from works WHERE workshop = AND attendee = ";
console.log("in the select statement");
const response = await pool.query(template, [ws, att]);
//if attendee and workshop already exists
if(response.rowCount != 0){
res.json({status: "attendee already enrolled"});
} else if (response.rowCount ==0){
//else if attendee and workshop doesn't exist
//INSERT the entry
const template2 = "INSERT INTO works(workshop, attendee) VALUES (, )";
console.log("in the insert statement");
const response1 = await pool.query(template2, [ws, att]);
res.json({status: "added"});
}
}catch (err){
console.log("Error.")
console.log(err);
}
})
app.listen(app.get("port"), () => {
console.log(`Find the server at: http://localhost:${app.get("port")}/`);
});
通过 Postman 传递的密钥是:
workshop : React Fundamentals
attendee : Claire Kim
但每次我尝试将这些值插入 table 时,我都会收到此错误:
error: relation "works" does not exist
参考线:
const template = "SELECT * from works WHERE workshop = AND attendee = ";
console.log("in the select statement");
const response = await pool.query(template, [ws, att]);
但是在 PSQL 中,当我输入 select 语句时,它工作正常并且 table“有效”。我的用户 'parky' 有权 select 并插入到“works”,还可以使用 select 按 works_id_seq table.
我已经为此工作了 6 个小时,但不知道出了什么问题。任何见解将不胜感激。
如果那是您用来创建 table 的实际 SQL 文件,那么您在名为 parky 的数据库中创建了 table。
\c parky
的输出应该是这样的:
You are now connected to database "parky" as user "postgres"
那一行应该是这样的:
\c project1 parky
我有一个非常基本的sql文件,代码:
\c postgres
DROP DATABASE IF EXISTS project1;
CREATE DATABASE project1;
\c parky
CREATE TABLE works (
id SERIAL PRIMARY KEY,
workshop TEXT NOT NULL,
attendee TEXT NOT NULL
);
这是我的 Javascript 服务器使用的:
require('dotenv').config()
const bodyParser = require("body-parser");
const express = require("express");
const app = express();
app.set("port", 8080);
app.use(bodyParser.json({ type: "application/json" }));
app.use(bodyParser.urlencoded({ extended: true }));
const Pool = require("pg").Pool;
const config = {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: "project1"
};
const pool = new Pool(config);
//Add an attendee to workshop
app.post("/api", async (req, res) => {
const ws = req.body.workshop;
const att = req.body.attendee;
try{
const template = "SELECT * from works WHERE workshop = AND attendee = ";
console.log("in the select statement");
const response = await pool.query(template, [ws, att]);
//if attendee and workshop already exists
if(response.rowCount != 0){
res.json({status: "attendee already enrolled"});
} else if (response.rowCount ==0){
//else if attendee and workshop doesn't exist
//INSERT the entry
const template2 = "INSERT INTO works(workshop, attendee) VALUES (, )";
console.log("in the insert statement");
const response1 = await pool.query(template2, [ws, att]);
res.json({status: "added"});
}
}catch (err){
console.log("Error.")
console.log(err);
}
})
app.listen(app.get("port"), () => {
console.log(`Find the server at: http://localhost:${app.get("port")}/`);
});
通过 Postman 传递的密钥是:
workshop : React Fundamentals
attendee : Claire Kim
但每次我尝试将这些值插入 table 时,我都会收到此错误:
error: relation "works" does not exist
参考线:
const template = "SELECT * from works WHERE workshop = AND attendee = ";
console.log("in the select statement");
const response = await pool.query(template, [ws, att]);
但是在 PSQL 中,当我输入 select 语句时,它工作正常并且 table“有效”。我的用户 'parky' 有权 select 并插入到“works”,还可以使用 select 按 works_id_seq table.
我已经为此工作了 6 个小时,但不知道出了什么问题。任何见解将不胜感激。
如果那是您用来创建 table 的实际 SQL 文件,那么您在名为 parky 的数据库中创建了 table。
\c parky
的输出应该是这样的:
You are now connected to database "parky" as user "postgres"
那一行应该是这样的:
\c project1 parky