如何在 1 Rails 处重新启动记录 id
我有一个 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
我无法删除数据库,因为某些表中将包含用户数据。
当我重新为表播种时,我需要将 id 重新启动为 1。
我怎样才能在 Rails 中做到这一点?
I have a rake task to populate a table.
as follows:
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
I can't drop the database as some tables will have user data in them.
I need to restart the id's at 1 when I re-seed the tables.
How can I do this in Rails?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 PostgreSql,要重新启动序列:
您还可以通过模型连接来执行此操作:
If you are using PostgreSql, to restart your sequence:
You can also do that through the model connection:
对于 mysql 尝试这个片段
For mysql try this snippet