# Auto-increment lost during migration

**URL:** <https://www.ruby-forum.com/t/auto-increment-lost-during-migration/74565>\
**Category:** Rails\
**Created:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/auto-increment-lost-during-migration/74565 "2006-11-07T10:48:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tobias\_Numiranta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4491bb/32.png) [@Tobias\_Numiranta](https://www.ruby-forum.com/u/Tobias_Numiranta)\
**Post date:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/auto-increment-lost-during-migration/74565/1 "2006-11-07T10:48:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tobias\_Numiranta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4491bb/32.png) [@Tobias\_Numiranta](https://www.ruby-forum.com/u/Tobias_Numiranta)\
**Post date:** [November 7, 2006, 10:49am UTC](https://www.ruby-forum.com/t/auto-increment-lost-during-migration/74565/2 "2006-11-07T10:49:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Tobias\_Numiranta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4491bb/32.png) [@Tobias\_Numiranta](https://www.ruby-forum.com/u/Tobias_Numiranta)\
**Post date:** [November 7, 2006, 10:49am UTC](https://www.ruby-forum.com/t/auto-increment-lost-during-migration/74565/3 "2006-11-07T10:49:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tobias\_Numiranta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4491bb/32.png) [@Tobias\_Numiranta](https://www.ruby-forum.com/u/Tobias_Numiranta)\
**Post date:** [November 7, 2006, 10:49am UTC](https://www.ruby-forum.com/t/auto-increment-lost-during-migration/74565/4 "2006-11-07T10:49:29Z")

</div>

Tobias Numiranta wrote:

> 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?

---

<div class="post-metadata">

**Author:** ![Tobias\_Numiranta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/4491bb/32.png) [@Tobias\_Numiranta](https://www.ruby-forum.com/u/Tobias_Numiranta)\
**Post date:** [November 7, 2006, 10:51am UTC](https://www.ruby-forum.com/t/auto-increment-lost-during-migration/74565/5 "2006-11-07T10:51:08Z")

</div>

On Oct 31, 6:14 pm, “Phrogz” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 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?
