diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 5402918b1d5bb74ff7accec91823cc3bae3779c2..d8598432607a607ca3242e2f421a99276a3ed5a3 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -1035,13 +1035,14 @@ def exec_no_cache(sql, binds) end def exec_cache(sql, binds) - unless @statements.key? sql + sql_key = "#{schema_search_path}-#{sql}" + unless @statements.key? sql_key nextkey = @statements.next_key @connection.prepare nextkey, sql - @statements[sql] = nextkey + @statements[sql_key] = nextkey end - key = @statements[sql] + key = @statements[sql_key] # Clear the queue @connection.get_last_result diff --git a/activerecord/test/cases/adapters/postgresql/schema_test.rb b/activerecord/test/cases/adapters/postgresql/schema_test.rb index 76c73e9dfa13a0f2ac4497d9c2f2da981e124314..b01eabc840f9a0e22f7a00cbb20fb4df8fb6e987 100644 --- a/activerecord/test/cases/adapters/postgresql/schema_test.rb +++ b/activerecord/test/cases/adapters/postgresql/schema_test.rb @@ -38,6 +38,10 @@ class Thing4 < ActiveRecord::Base set_table_name 'test_schema."Things"' end + class Thing5 < ActiveRecord::Base + set_table_name 'things' + end + def setup @connection = ActiveRecord::Base.connection @connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})" @@ -236,6 +240,21 @@ def test_current_schema end end + def test_prepared_statements_with_multiple_schemas + + @connection.schema_search_path = SCHEMA_NAME + Thing5.create(:id => 1, :name => "thing inside #{SCHEMA_NAME}", :email => "thing1@localhost", :moment => Time.now) + + @connection.schema_search_path = SCHEMA2_NAME + Thing5.create(:id => 1, :name => "thing inside #{SCHEMA2_NAME}", :email => "thing1@localhost", :moment => Time.now) + + @connection.schema_search_path = SCHEMA_NAME + assert_equal 1, Thing5.count + + @connection.schema_search_path = SCHEMA2_NAME + assert_equal 1, Thing5.count + end + def test_schema_exists? { 'public' => true,