I am currently trying to do a complicated WHERE search on a table using Rails, the trouble is I get the error:
PG::Error: ERROR: column "email" does not exist
LINE 1: SELECT "bans".* FROM "bans" WHERE (Email='' AND IP='' AND (...
^
: SELECT "bans".* FROM "bans" WHERE (Email='' AND IP='' AND (Username='NULL' ))
And I know that column actually exists, and doing a rails dbconsole gives me the following:
Jungle=> select * from bans;
id | Username | IP | Email | Reason | Length | created_at | updated_at
----+----------+----+-------+--------+--------+------------+------------
(0 rows)
So this is definatly in the database, has anyone had any experience with this?
SQL column names are case insensitive unless quoted, the standard says that identifiers should be normalized to upper case but PostgreSQL normalizes to lower case:
You’re referencing
Emailin your SQL:but PostgreSQL is complaining about
email:Your unquoted
Emailis being treated asemailbecause PostgreSQL normalizes identifiers to lower case. Sounds like you created the columns with capitalized names by double quoting them:or by using
:Emailto identify the column in a migration. If you quote a column name when it is created, then it is not normalized to lower case (or upper case in the SQL standard case) and you’ll have to double quote it and match the case forever:Once you fix
Email, you’ll have the same problem withIP,Username,Reason, andLength: you’ll have to double quote them all in any SQL that references them.The best practise is to use lower case column and table names so that you don’t have to worry about quoting things all the time. I’d recommend that you fix your table to have lower case column names.
As an aside, your
'NULL'string literal:looks odd, are you sure that you don’t mean
"Username" is null? The'NULL'string literal and the NULL value are entirely different things and you can’t use=or!=to compare things against NULL, you have to useis null,is not null,is distinct from, oris not distinct from(depending on your intent) when NULLs might be in play.