无法从Postgres打印updatedAT列

时间:2016-08-10 21:00:07

标签: python postgresql python-3.5

我在postgres中有一个表格,我想只获得updatedAt列。如果我打印表列,则updatedAt存在:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import psycopg2
import sys
import datetime

con = None

try:

    con = psycopg2.connect(database='db', user='dbuser', password='1111')
    cur = con.cursor()
    cur.execute('SELECT * from incidents')

    column_names = [row[0] for row in cur.description]

    print("Column names: %s\n" % column_names)


except psycopg2.DatabaseError as e:
    print('Error %s' % e)
    sys.exit(1)


finally:

    if con:
        con.close()

结果是

Column names: ['id', 'deviceId', 'latitude', 'longitude', 'city', 'area', 'address', 'state', 'postalCode', 'country', 'canceled', 'createdAt', 'updatedAt', 'patientId']

但是当我尝试从该表中选择所有行并仅获取updatedAt信息时,它表示该列不存在。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import psycopg2
import sys
import datetime

con = None

try:

    con = psycopg2.connect(database='db', user='dbuser', password='1111')
    cur = con.cursor()
    cur.execute('SELECT updatedAt from incidents')
    rows = cur.fetchall()

    for row in rows:
        print(row)


except psycopg2.DatabaseError as e:
    print('Error %s' % e)
    sys.exit(1)


finally:

    if con:
        con.close()

错误消息是:

Error column "updatedat" does not exist
LINE 1: SELECT updatedAt from incidents
               ^
HINT:  Perhaps you meant to reference the column "incidents.updatedAt".

我也试过了incidents.updatedAt,但没有运气。

修改

忘记提到这个表是使用Sequelize从Node.js创建的。

1 个答案:

答案 0 :(得分:2)

Postgres列名默认为小写。由于该列名称中包含大写字母,因此必须引用它才能被引用。如果列中有空格或特殊字符,情况也是如此。此查询应该有效:

SELECT "updatedAt" FROM incidents