0

我有一个 rake 任务来填充表格。

如下:

namespace :db do
  desc  "fill in the database with icons data"
  task :populate_icons => :environment do 
    make_types
    make_templates
    make_categories
    make_greeting_icons
    make_user_icons
  end
end

  def make_types
    Type.delete_all
    types = %w{Classic Popular Children Animals}
    types.each do |type|   
      Type.create!(:name => type, :adult => false) #1
    end
  end

我无法删除数据库,因为某些表中将包含用户数据。

当我重新播种表时,我需要在 1 处重新启动 id。

我怎样才能在 Rails 中做到这一点?

4

2 回答 2

2

如果您使用 PostgreSql,重新启动您的序列:

ALTER SEQUENCE table_id_seq RESTART 1;

您也可以通过模型连接来做到这一点:

MyModel.connection.execute('ALTER SEQUENCE my_models_id_seq RESTART 1')
于 2011-10-12T10:31:01.477 回答
1

对于 mysql 试试这个片段

ActiveRecord::Base.connection.execute('ALTER TABLE tablename AUTO_INCREMENT=1')
于 2011-10-12T10:28:43.300 回答