rails/rails — Issue #5094: Postgresql adapter has lost ability to detect sequences based upon primary key default values 
Author: lawso017
Created: 2012-02-20 01:13:32+00:00
State: open

After a Rails 3.0.10 -> 3.1.3 upgrade, noticed that my generated schema.rb file was no longer showing primary keys (i.e. was generating :id => false) for tables where the underlying Postgresql sequence no longer shared the name of the table.

This occurred in a long-running Rails app where these tables had been renamed during refactorings. As a result, the name of the sequence was encoded in the table's primary key default value and no longer corresponds to the table.

The below well-intentioned commit:

https://github.com/rails/rails/commit/35dba50be9f9641ba757e6253b6e39f4b92dd37f#activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

removed the decision branch from pk_and_sequence_for necessary to infer sequence names from a primary key's default value.

The method pk_and_sequence_for needs to be restored to the state prior to this commit. The logic removed is not specific to Postgres < 8.2, but rather compatible with Postgres > 8.1.

This is my first time spelunking through the AR code. I'll fork and see if I can figure out how to test this change, but otherwise am monkey-patching to get things running again.

The change above affects 3.1 stable as well as Master.