add id_seq reset for PostgreSQL databases

This commit is contained in:
Kevin Triplett 2011-08-25 00:00:30 -05:00
parent f1190e06c8
commit 96c046d483

View File

@ -24,4 +24,13 @@ class ActiveRecord::Base
end
record
end
# for PostgreSQL (and others?) we must reset the id sequential to the next
# available highest number. This happens because the create_or_update
# creates records with an id already set, bypassing PostgreSQL's automatic
# incrementing sequential id counter
# Otherwise, we get the dreaded "duplicate key value violates unique constraint" error
def self.postgresql_reset_id_seq
connection.execute("SELECT setval('#{self.table_name}_id_seq'::regclass, MAX(id)) FROM #{self.table_name}")
end
end