Rails修改auto_increment值

在Rails的Migration中修改数据表的auto_increment值,示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class AlterUsertype < ActiveRecord::Migration
 def self.up
   execute "update usertypes set position = position + 1 where position > 4"
   us = Usertype.create(:name => 'virtual_school')
   # 要单独设置position属性,是因为Usertype模型中使用了acts_as_list的特性
   us.position = 5
   us.save
 end
 
 def self.down
  Usertype.delete_all(['name = ?', 'virtual_school'])
  execute "update usertypes set position = position - 1 where position > 4"
  # 更新usertypes表的auto_increment属性
  execute "alter table usertypes auto_increment = 12"
 end
end

Monitor Your Web Site 24/7 - Receive email and SMS alerts anytime your web site goes down.

Related Posts

Leave a Reply

Monitor Your Web Site 24/7 - Receive email and SMS alerts anytime your web site goes down.

About Me

About Me
Record Technical Articles at Gooss.org.

Entries (RSS) and Comments (RSS). Valid XHTML and CSS and XFN.

© Copyright 2009 山海经. All rights reserved.

Social Bookmarks

Copyright © Designed by: Free WP Templates
Food Charts |  About Breast Milk |  Organic Food