<class 'psycopg2.errors.UndefinedColumn'>:JOIN 时列不存在

问题描述 投票:0回答:1
  • python:3.9.17
  • psycopg2:2.9.7
  • 操作系统:Ubuntu 20.0
  • postgre:12

我已尽力而为,但仍然出现此错误:

2023-09-11 22:45:35,033 ERROR <class 'psycopg2.errors.UndefinedColumn'>: column u.id does not exist
LINE 1: ...id, u.balance FROM key k JOIN user u ON k.user_id=u.id WHERE...
                                                             ^

Python代码:

# this all below return the same error
# sql = """SELECT k.user_id, k.custom_rates, u.id, u.balance FROM key k JOIN user u ON k.user_id=u.id WHERE k.key='test'"""
# sql = "SELECT k.user_id, k.custom_rates, u.id, u.balance FROM user u JOIN key k ON k.user_id=u.id WHERE k.key='test'"
# sql = "SELECT k.user_id, k.custom_rates, u.id, u.balance FROM key k INNER JOIN user u ON k.user_id=u.id WHERE k.key='test'"
sql = "SELECT k.user_id, k.custom_rates, u.id, u.balance FROM key k JOIN user u ON k.user_id=u.id WHERE k.key='test'"
cursor.execute(sql)
res = cursor.fetchone()

keyuser 表:

什么是错误?

python postgresql psycopg2 psql
1个回答
0
投票

key 和 user 是保留字。尝试:

FROM "key" k JOIN "user" u
© www.soinside.com 2019 - 2024. All rights reserved.