I have a Rails app using a Postgres database with a table called geolite_blocks. If I call ActiveRecord like this:
GeoliteBlock.find_by_startIpNum 2776360991
The query works perfectly. However, if I do the query like this:
GeoliteBlock.where("startIpNum >= ?", 2776360991)
I get this error:
ActiveRecord::StatementInvalid: PGError: ERROR: column "startipnum" does not exist
LINE 1: ... "geolite_blocks".* FROM "geolite_blocks" WHERE (startIpNum...
^
: SELECT "geolite_blocks".* FROM "geolite_blocks" WHERE (startIpNum >= 2776360991)
But I know that the column exists because I just queried by it with the first code example. Any ideas as to why this might be happening, and how I can eliminate it? Thanks for any help!
Column names in SQL are case insensitive unless they were quoted when they were created. Someone created your startIpNum
column with quotes around it so you have to quote it every time you use it:
GeoliteBlock.where('"startIpNum" >= ?', 2776360991)
The error you're getting from PostgreSQL mentions startipnum
because PostgreSQL normalizes identifiers to lower case (the SQL standard says that they should be normalized to upper case though).
This:
GeoliteBlock.find_by_startIpNum 2776360991
works because AR will quote the startIpNuM
part behind your back. Similarly, GeoliteBlock.where(:startIpNum => 2776360991)
would also work.
I'd recommend that you change the schema to use lower case column names so that you won't have to worry about this anymore.