[英]pg node + POSTGRES column name does not exist
我正在使用 pg 和 nodejs 來制作一個簡單的后端數據庫。 當我嘗試查詢表時,我收到一條錯誤消息,指出列名不存在。 然而,當我執行 SQL 查詢以查找表中的所有列名時,它會顯示具有相同名稱的列。
這是我用來查詢帶有列字段的表的方法:
const query = await db.query('SELECT * FROM user WHERE email = $1', [email]);
這是執行該查詢的錯誤消息:
error: column "email" does not exist
這是在數據庫中查找表的列的響應:
{
table_catalog: 'recipe-book',
table_schema: 'public',
table_name: 'user',
column_name: 'email',
ordinal_position: 2,
column_default: null,
is_nullable: 'NO',
data_type: 'character varying',
character_maximum_length: 255,
character_octet_length: 1020,
numeric_precision: null,
numeric_precision_radix: null,
numeric_scale: null,
datetime_precision: null,
interval_type: null,
interval_precision: null,
character_set_catalog: null,
character_set_schema: null,
character_set_name: null,
collation_catalog: null,
collation_schema: null,
collation_name: null,
domain_catalog: null,
domain_schema: null,
domain_name: null,
udt_catalog: 'recipe-book',
udt_schema: 'pg_catalog',
udt_name: 'varchar',
scope_catalog: null,
scope_schema: null,
scope_name: null,
maximum_cardinality: null,
dtd_identifier: '2',
is_self_referencing: 'NO',
is_identity: 'NO',
identity_generation: null,
identity_start: null,
identity_increment: null,
identity_maximum: null,
identity_minimum: null,
identity_cycle: 'NO',
is_generated: 'NEVER',
generation_expression: null,
is_updatable: 'YES'
},
我以前用這種方式使用過pg和node,從來沒有見過這個錯誤,是我做錯了什么嗎?
實際上,postgres 中有一個user
function 顯示數據庫上的當前用戶。
因此,在您的情況下,它選擇的是 postgres 用戶表而不是您自己的用戶表。 盡管您在連接設置中指定了數據庫,但仍將檢索當前用戶。
要擺脫它,請重命名表或使用雙引號來指示差異。
const query = await db.query(`SELECT * FROM "user" WHERE email = $1`, [email]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.