--- postgresql_adapter.rb.orig 2010-02-25 14:42:49.000000000 -0800 +++ postgresql_adapter.rb 2010-02-25 15:45:32.000000000 -0800 @@ -764,7 +764,7 @@ end if pk if sequence - quoted_sequence = quote_column_name(sequence) + quoted_sequence = quote_table_name(sequence) select_value <<-end_sql, 'Reset sequence' SELECT setval('#{quoted_sequence}', (SELECT COALESCE(MAX(#{quote_column_name pk})+(SELECT increment_by FROM #{quoted_sequence}), (SELECT min_value FROM #{quoted_sequence})) FROM #{quote_table_name(table)}), false) @@ -818,8 +818,26 @@ end_sql end - # [primary_key, sequence] - [result.first, result.last] + primary_key = result.first + sequence = result.last + + if ! sequence.blank? + result = query(<<-end_sql, 'Find sequence schema if any')[0] + SELECT name.nspname + FROM pg_class seq, + pg_namespace name, + pg_depend dep + WHERE seq.relname = '#{sequence}' + AND seq.oid = dep.objid + AND dep.refobjid = '#{quote_table_name(table)}'::regclass + AND seq.relnamespace = name.oid + end_sql + if ! result.nil? and ! result.empty? + sequence = result.first + "." + sequence + end + end + + [primary_key, sequence] rescue nil end