8

Getting an operator mismatch error when doing a simple query. What causes this?

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.

Table definition:

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

1 Answer 1

14

According to the manual, USER is a reserved keyword. You must quote it to avoid the syntax error.

SELECT * FROM registrants WHERE "user" = 1

PostgreSQL Reserved Keyword List

If you have time to alter the database, change the column name to one which is not a reserved keyword. This will help you avoid future headaches.

Sign up to request clarification or add additional context in comments.

2 Comments

@user2254435: Don't use reserved words as identifiers to begin with.
Thank you for the quick response. This is from a large existing application and not so easy to change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.