无法 运行 在 node-postgres 中进行简单的 WHERE 查询
Cannot run simple WHERE query in node-postgres
这是我的 table:
-- Table Definition ----------------------------------------------
CREATE TABLE "Friendships" (
id integer DEFAULT nextval('"Friendships_id_seq"'::regclass) PRIMARY KEY,
"createdAt" timestamp with time zone NOT NULL,
"updatedAt" timestamp with time zone NOT NULL,
"fromUserId" integer NOT NULL REFERENCES "Users"(id),
"toUserId" integer NOT NULL REFERENCES "Users"(id)
);
-- Indices -------------------------------------------------------
CREATE UNIQUE INDEX "Friendships_pkey" ON "Friendships"(id int4_ops);
这是我的查询:
const { Client } = require('pg')
const pgClient = new Client({ user: PG_USERNAME, host: PG_HOST, database: PG_DB, password: PG_PASSWORD, port: 5432 })
pgClient.connect()
pgClient.query({
name: 'fetch-friendships',
text: 'SELECT * FROM Friendships WHERE fromUserId = ',
values: [1]
}, (err, res) => {
console.log(err)
console.log(res)
})
错误:relation "friendships" does not exist
cosmos=> \dt;
List of relations
Schema | Name | Type | Owner
--------+-----------------+-------+-------
public | AuthCodes | table | pangu
public | BirthRecords | table | pangu
public | Births | table | pangu
public | Friendships | table | pangu
public | InvitationCodes | table | pangu
public | SequelizeMeta | table | pangu
public | Users | table | pangu
(7 rows)
cosmos=> SELECT * FROM Friendships;
ERROR: relation "friendships" does not exist
LINE 1: SELECT * FROM Friendships;
在 postgres 中,如果使用大写字母创建 table,则需要使用双引号。
没有双引号
Friendships -> friendships
正如您所看到的错误:
ERROR: relation "friendships" does not exist
但你创造了
CREATE TABLE "Friendships" (
所以你需要使用"Friendships"
同样适用于表、字段名称或函数名称。
这是我的 table:
-- Table Definition ----------------------------------------------
CREATE TABLE "Friendships" (
id integer DEFAULT nextval('"Friendships_id_seq"'::regclass) PRIMARY KEY,
"createdAt" timestamp with time zone NOT NULL,
"updatedAt" timestamp with time zone NOT NULL,
"fromUserId" integer NOT NULL REFERENCES "Users"(id),
"toUserId" integer NOT NULL REFERENCES "Users"(id)
);
-- Indices -------------------------------------------------------
CREATE UNIQUE INDEX "Friendships_pkey" ON "Friendships"(id int4_ops);
这是我的查询:
const { Client } = require('pg')
const pgClient = new Client({ user: PG_USERNAME, host: PG_HOST, database: PG_DB, password: PG_PASSWORD, port: 5432 })
pgClient.connect()
pgClient.query({
name: 'fetch-friendships',
text: 'SELECT * FROM Friendships WHERE fromUserId = ',
values: [1]
}, (err, res) => {
console.log(err)
console.log(res)
})
错误:relation "friendships" does not exist
cosmos=> \dt;
List of relations
Schema | Name | Type | Owner
--------+-----------------+-------+-------
public | AuthCodes | table | pangu
public | BirthRecords | table | pangu
public | Births | table | pangu
public | Friendships | table | pangu
public | InvitationCodes | table | pangu
public | SequelizeMeta | table | pangu
public | Users | table | pangu
(7 rows)
cosmos=> SELECT * FROM Friendships;
ERROR: relation "friendships" does not exist
LINE 1: SELECT * FROM Friendships;
在 postgres 中,如果使用大写字母创建 table,则需要使用双引号。
没有双引号
Friendships -> friendships
正如您所看到的错误:
ERROR: relation "friendships" does not exist
但你创造了
CREATE TABLE "Friendships" (
所以你需要使用"Friendships"
同样适用于表、字段名称或函数名称。