PostgreSQL column 'foo' does not exist

前端 未结 10 2125
盖世英雄少女心
盖世英雄少女心 2020-12-05 12:32

I have a table that has 20 integer columns and 1 text column named \'foo\'

If I run query:

SELECT * from table_name where foo is NULL
10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 13:16

    As others suggested in comments, this is probably a matter of upper-case versus lower-case, or some whitespace in the column name. (I'm using an answer so I can format some code samples.) To see what the column names really are, try running this query:

    SELECT '"' || attname || '"', char_length(attname)
      FROM pg_attribute
      WHERE attrelid = 'table_name'::regclass AND attnum > 0
      ORDER BY attnum;
    

    You should probably also check your PostgreSQL server log if you can, to see what it reports for the statement.

    If you quote an identifier, everything in quotes is part of the identifier, including upper-case characters, line endings, spaces, and special characters. The only exception is that two adjacent quote characters are taken as an escape sequence for one quote character. When an identifier is not in quotes, all letters are folded to lower-case. Here's an example of normal behavior:

    test=# create table t (alpha text, Bravo text, "Charlie" text, "delta " text);
    CREATE TABLE
    test=# select * from t where Alpha is null;
     alpha | bravo | Charlie | delta  
    -------+-------+---------+--------
    (0 rows)
    
    test=# select * from t where bravo is null;
     alpha | bravo | Charlie | delta  
    -------+-------+---------+--------
    (0 rows)
    
    test=# select * from t where Charlie is null;
    ERROR:  column "charlie" does not exist
    LINE 1: select * from t where Charlie is null;
                                  ^
    test=# select * from t where delta is null;
    ERROR:  column "delta" does not exist
    LINE 1: select * from t where delta is null;
                                  ^
    

    The query I showed at the top yields this:

     ?column?  | char_length 
    -----------+-------------
     "alpha"   |           5
     "bravo"   |           5
     "Charlie" |           7
     "delta "  |           6
    (4 rows)
    

提交回复
热议问题