Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

disregard for schema search path in postgresql_adapter.rb

#6457

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.

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

State: new
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. SooDesuNe
    SooDesuNe

    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
    

    February 26th, 2011 @ 09:11 PM