【问题标题】:postgres: relation does not exist when the table clearly exists when calling from node.jspostgres:从node.js调用时表明显存在时关系不存在
【发布时间】:2020-08-06 07:59:46
【问题描述】:

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

 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);

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

【问题讨论】:

  • 显然没有默认模式集,是吗?
  • 不确定默认架构是什么意思?我尝试使用public.users 创建表,但出现相同的错误消息。我尝试了SELECT pg_catalog.set_config('search_path', '', true);SET search_path TO schema,public; 之类的所有方法,但仍然遇到同样的问题。
  • 数据库名称是什么?可以分享一下代码执行的查询吗?
  • 制作values: [ ['id1': 'username1'], ['id2', 'username2'], ... ]; 然后,db.query("INSERT INTO users(id, username) VALUES ?", [values], callback)。检查它是否有效...

标签: javascript sql node.js postgresql node-postgres


【解决方案1】:

最可能的选择是表是在与您在 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)

【讨论】:

    【解决方案2】:

    默认情况下,当您不指定任何架构时,postgres 会指向公共架构。请使用模式名称/数据库名称以及表名称。确保您在代码中提供了正确的数据库配置。如果配置正确,甚至您不需要在查询中提供架构别名。

    【讨论】:

      猜你喜欢
      • 2018-06-12
      • 2019-04-23
      • 2019-08-02
      • 2020-04-09
      • 2018-12-02
      • 1970-01-01
      • 2018-05-15
      • 2015-08-13
      • 1970-01-01
      相关资源
      最近更新 更多