Auto-increment lost during migration.

Hi. I get some strange results when using rename_column on a primary
key in a migration. It seems like the migration script removes the
auto-increment property if you rename a column.

This is a minimal example.

    create_table :foo, :primary_key => :foo_id do |t|
      t.column "name", :string
    end

    # renaming the primary key makes auto-increment disappear.
    rename_column :foo, :foo_id, :bar_id

Is this a known problem, any beautiful solution available? Maybe use
change_column instead?

Best regards,
Tobias Nurmiranta

Tobias Numiranta wrote:

    create_table :foo, :primary_key => :foo_id do |t|
      t.column "name", :string
    end

    # renaming the primary key makes auto-increment disappear.
    rename_column :foo, :foo_id, :bar_id

What RDBMS are you using? Can you show what the table schema looks like
(as reported by the rdbms) before and after the change?

Sure. I'm using Mysql. Using mysqldump before and after I get:

before:

CREATE TABLE `foo` (
  `foo_id` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  PRIMARY KEY (`foo_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

after:

CREATE TABLE `foo` (
  `bar_id` int(11) NOT NULL default '0',
  `name` varchar(255) default NULL,
  PRIMARY KEY (`bar_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Have a nice day,
T

Tobias Numiranta wrote:

Sure. I'm using Mysql. Using mysqldump before and after I get:

before:

CREATE TABLE `foo` (
  `foo_id` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  PRIMARY KEY (`foo_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

after:

CREATE TABLE `foo` (
  `bar_id` int(11) NOT NULL default '0',
  `name` varchar(255) default NULL,
  PRIMARY KEY (`bar_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Interesting. I don't use MySQL. What do the logs show as the SQL being
issued to MySQL for the change_column migration?

Hi. This is the issued SQL:

-- create_table(:foo, {:primary_key=>:foo_id})
CREATE TABLE foo (`foo_id` int(11) DEFAULT NULL auto_increment PRIMARY
KEY, `name` varchar(255)) ENGINE=InnoDB

-- rename_column(:foo, :foo_id, :bar_id)
SHOW COLUMNS FROM foo LIKE 'foo_id'
ALTER TABLE foo CHANGE foo_id bar_id int(11)

Maybe the alter table command need to specify auto_increment, but rails
doesn't do so?