2013-04-07 44 views
6

執行簡單查詢時出現操作符不匹配錯誤。這是什麼原因?PostgreSQL錯誤:操作符不存在:name =整數

 
dev_db=# `select * from registrants where user=1;` 
ERROR: operator does not exist: name = integer 
LINE 1: select * from registrants where user=1; 
              ^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 

表定義:

 
dev_db=# \d+ registrants 
           Table "public.registrants" 
    Column |   Type   |  Modifiers  | Storage | Description 
--------------+--------------------------+--------------------+----------+------------- 
user   | integer     | not null   | plain | 
degree  | text      |     | extended | 
title  | text      |     | extended | 
organization | text      |     | extended | 
address  | text      |     | extended | 
city   | text      |     | extended | 

Indexes: 
    "registrants_pkey" PRIMARY KEY, btree ("user") 
Foreign-key constraints: 
    "registrants_country_fkey" FOREIGN KEY (country) REFERENCES countries(id) 
    "registrants_user_fkey" FOREIGN KEY ("user") REFERENCES users(id) 
Referenced by: 
    TABLE "class_evaluations" CONSTRAINT "class_evaluations_registrant_fkey" FOREIGN KEY (registrant) REFERENCES registrants("user") 

Triggers: 
    archive_registrants BEFORE DELETE OR UPDATE ON registrants FOR EACH ROW EXECUTE PROCEDURE archive_reg_table() 
Has OIDs: no 

回答

7

根據手冊,USER是保留關鍵字。您必須引用它以避免語法錯誤。

SELECT * FROM registrants WHERE "user" = 1 

PostgreSQL Reserved Keyword List

如果你有時間來修改數據庫,更改列名其中之一是不是保留關鍵字。這將幫助您避免未來的麻煩。

+3

@ user2254435:不要使用保留字作爲開頭的標識符。 – 2013-04-07 12:34:38

+0

感謝您的快速回復。這是來自現有的大型應用程序,不容易更改。 – DevR 2013-04-07 12:40:52

相關問題