aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2005-10-18 19:46:11 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2005-10-18 19:46:11 +0000
commit311342d8e231f8429e270dce2b1bf4abeb7b4293 (patch)
treea325b6a51da37a0c1b905777dbd3ce1e70ae7ef4 /activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
parent86bdbc3a62ac125f6928d27bf743fec6c0cca81d (diff)
downloadrails-311342d8e231f8429e270dce2b1bf4abeb7b4293.tar.gz
rails-311342d8e231f8429e270dce2b1bf4abeb7b4293.tar.bz2
rails-311342d8e231f8429e270dce2b1bf4abeb7b4293.zip
Correct PostgreSQL primary key sequence detection. #2507
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2680 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb33
1 files changed, 20 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 203d390046..ec02110f6e 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -205,8 +205,8 @@ module ActiveRecord
# Set the sequence to the max value of the table's pk.
def reset_pk_sequence!(table)
- sequence, pk = sequence_and_pk_for(table)
- if sequence and pk
+ pk, sequence = pk_and_sequence_for(table)
+ if pk and sequence
select_value <<-end_sql, 'Reset sequence'
SELECT setval('#{sequence}', (SELECT COALESCE(MAX(#{pk})+(SELECT increment_by FROM #{sequence}), (SELECT min_value FROM #{sequence})) FROM #{table}), false)
end_sql
@@ -214,19 +214,26 @@ module ActiveRecord
end
# Find a table's primary key and sequence.
- def sequence_and_pk_for(table, column = nil)
+ def pk_and_sequence_for(table)
execute(<<-end_sql, 'Find pk sequence')[0]
- SELECT (name.nspname || '.' || seq.relname) AS sequence, attr.attname AS pk
- FROM pg_class seq, pg_attribute attr, pg_depend dep, pg_namespace name, pg_constraint cons
- WHERE seq.oid = dep.objid
- AND seq.relnamespace = name.oid
- AND attr.attrelid = dep.refobjid
- AND attr.attnum = dep.refobjsubid
- AND attr.attrelid = cons.conrelid
- AND attr.attnum = cons.conkey[1]
- AND cons.contype = 'p'
- AND dep.refobjid = '#{table}'::regclass
+ SELECT attr.attname, (name.nspname || '.' || seq.relname)
+ FROM pg_class seq,
+ pg_attribute attr,
+ pg_depend dep,
+ pg_namespace name,
+ pg_constraint cons
+ WHERE seq.oid = dep.objid
+ AND seq.relnamespace = name.oid
+ AND seq.relkind = 'S'
+ AND attr.attrelid = dep.refobjid
+ AND attr.attnum = dep.refobjsubid
+ AND attr.attrelid = cons.conrelid
+ AND attr.attnum = cons.conkey[1]
+ AND cons.contype = 'p'
+ AND dep.refobjid = '#{table}'::regclass
end_sql
+ rescue
+ nil
end