简体   繁体   中英

rails SQL query reported failing with postgres but succeeds from psql command-line tool

I am trying to use a Rails 2.3.5 app with Postgres9.1 on Ubuntu (deployed on Apache2 with Phusion Passenger). The app throws this error when I try to access it, complaining that table does not exist:

PGError: ERROR:  relation "users" does not exist
LINE 4:              WHERE a.attrelid = '"users"'::regclass
                                    ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"users"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum

However if I cut and paste the same query at the psql (cmdline-client), it works just fine. The users table exists too. I tried a rake db:reset and 'rake db:migrate' again and again but it doesn't seem to help.
I also verified that the ActiveRecord::Base.connection succeeds from the script/console in the rails-appwhich means the connection to the database works with the credentials configured in database.yml.

I am lost.. any clues to remedy this will be appreciated greatly.

Three possible causes.

1) The special cast ::regclass takes the current setting for search_path into account. Maybe your table users is in a schema that is not in the search_path when querying from your app. search_path can be set per user or session.
Solution would be to schema-qualify the table-name like this:

'myschema.users'::regclass

2) Capitalization. Why '"users"'::regclass and not 'users'::regclass ? This is redundant. Is the actual name of the Table "Users" or something and you use upper case in psql? (If you actually cut & paste the query, this can't be it.)

3) Connection to the wrong database. Wrong port? (See comment by @a_horse.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM