diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index 4b5e077aaa092..ccd88144069da 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -528,8 +528,10 @@ def check_constraints(table_name) # :nodoc: SELECT conname, pg_get_constraintdef(c.oid, true) AS constraintdef, c.convalidated AS valid FROM pg_constraint c JOIN pg_class t ON c.conrelid = t.oid + JOIN pg_namespace n ON n.oid = c.connamespace WHERE c.contype = 'c' AND t.relname = #{scope[:name]} + AND n.nspname = #{scope[:schema]} SQL check_info.map do |row| diff --git a/activerecord/test/cases/migration/check_constraint_test.rb b/activerecord/test/cases/migration/check_constraint_test.rb index a7d119ea7209b..ef71281bc7698 100644 --- a/activerecord/test/cases/migration/check_constraint_test.rb +++ b/activerecord/test/cases/migration/check_constraint_test.rb @@ -58,6 +58,22 @@ def test_check_constraints end end + if current_adapter?(:PostgreSQLAdapter) + def test_check_constraints_scoped_to_schemas + @connection.add_check_constraint :trades, "quantity > 0" + + assert_no_changes -> { @connection.check_constraints("trades").size } do + @connection.create_schema "test_schema" + @connection.create_table "test_schema.trades" do |t| + t.integer :quantity + end + @connection.add_check_constraint "test_schema.trades", "quantity > 0" + end + ensure + @connection.drop_schema "test_schema" + end + end + def test_add_check_constraint @connection.add_check_constraint :trades, "quantity > 0"