javascript - postgres : relation does not exist when the table clearly exists when calling from node. js

标签 javascript sql node.js postgresql node-postgres

所以我 psql 并创建了表用户;

 CREATE TABLE users (
    id integer NOT NULL,
    username text
);

我可以通过 SELECT * FROM users;

获取行

但是,当我使用带有库模块 pg 的 node.js 进行调用时,我得到了臭名昭著的关系不存在。

const createQuery = {text: "INSERT INTO users(id, username) VALUES($1)",values: values}
const { rows } = await db.query(createQuery);

在完成服务器迁移之前,我没有遇到过这个问题。

最佳答案

最可能的选择是该表是在与您从 node.js 使用的默认模式不同的模式中创建的。

您可以通过查询表 information_schema.tables 来搜索您的表所属的架构:

select table_schema from information_schema.tables where table_name = 'users';

有了这些信息,您现在可以在从 node.js 运行的查询中使用正确的模式名称来限定您的表名。假设模式名称是 myschema,您将:

INSERT INTO myschema.users(id, username) VALUES($1)

关于javascript - postgres : relation does not exist when the table clearly exists when calling from node. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61379682/

相关文章:

node.js - 如何检查nodejs中的标题?

node.js - 如何在nodejs中为http.request设置自定义超时?

node.js - MongoError : Topology is closed, 尽管已建立数据库连接,但请连接

javascript - React 是如何在不需要 jQuery 的情况下允许 $.ajax 的?

php - 使用 jQuery 滑动一个 div 打开一个关闭

javascript - 单个表单按钮错误地触发两个事件监听器和警报框

mysql - 如何使用 IF() 条件在 MySQL 中显示 true 和 false 行

javascript - 是否可以通过 Chrome 以编程方式创建虚拟 MIDI 设备?

MYSQL 从重复条目中获取最高值

php - 加入三个不同的表