This project is archived and is in readonly mode.

#6457 new
SooDesuNe

disregard for schema search path in postgresql_adapter.rb

Reported by SooDesuNe | February 21st, 2011 @ 05:38 AM

in #table_exists? the SQL to determine if the table exists is:

    query(<<-SQL).first[0].to_i > 0
        SELECT COUNT(*)
        FROM pg_tables
        WHERE tablename = '#{table.gsub(/(^"|"$)/,'')}'
        #{schema ? "AND schemaname = '#{schema}'" : ''}

this finds all tables in the database, regardless of whether or not they are in the current schema search path. #table_exists? is frequently used to determine if tables should/can be dropped (as in active_record/connection_adapters/abstract/schema_statements.rb #create_table).

Postgres will not let you drop a table that's not in your current search path. PGError: ERROR: relation does not exist is raised.

I believe #table_exists? should return false if the table is not in the current search path.

Comments and changes to this ticket

  • SooDesuNe

    SooDesuNe February 26th, 2011 @ 09:11 PM

    One possible solution looks like:

        # `AND schemaname = ANY (current_schemas(false))` added so only tables in the current search path are included
        query(<<-SQL).first[0].to_i > 0
            SELECT COUNT(*)
            FROM pg_tables
            WHERE tablename = '#{table.gsub(/(^"|"$)/,'')}'
            #{schema ? "AND schemaname = '#{schema}'" : ''}
            AND schemaname = ANY (current_schemas(false))
        SQL
    

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages