Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6704417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:17:10+00:00 2026-05-26T07:17:10+00:00

I have found the pure SQL answers to this question. Is there a way,

  • 0

I have found the “pure SQL” answers to this question. Is there a way, in Rails, to reset the id field for a specific table?
Why do I want to do this? Because I have tables with constantly moving data – rarely more than 100 rows, but always different. It is up to 25k now, and there’s just no point in that. I intend on using a scheduler internal to the Rails app (rufus-scheduler) to run the id field reset monthly or so.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:17:11+00:00Added an answer on May 26, 2026 at 7:17 am

    I came out with a solution based on hgimenez’s answer and this other one.

    Since I usually work with either Sqlite or PostgreSQL, I’ve only developed for those; but extending it to, say MySQL, shouldn’t be too troublesome.

    Put this inside lib/ and require it on an initializer:

    # lib/active_record/add_reset_pk_sequence_to_base.rb
    module ActiveRecord
      class Base
        def self.reset_pk_sequence
          case ActiveRecord::Base.connection.adapter_name
          when 'SQLite'
            new_max = maximum(primary_key) || 0
            update_seq_sql = "update sqlite_sequence set seq = #{new_max} where name = '#{table_name}';"
            ActiveRecord::Base.connection.execute(update_seq_sql)
          when 'PostgreSQL'
            ActiveRecord::Base.connection.reset_pk_sequence!(table_name)
          else
            raise "Task not implemented for this DB adapter"
          end
        end     
      end
    end
    

    Usage:

    Client.count # 10
    Client.destroy_all
    Client.reset_pk_sequence
    Client.create(:name => 'Peter') # this client will have id=1
    

    EDIT: Since the most usual case in which you will want to do this is after clearing a database table, I recommend giving a look to database_cleaner. It handles the ID resetting automatically. You can tell it to delete just selected tables like this:

    DatabaseCleaner.clean_with(:truncation, :only => %w[clients employees])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found that there is generally a singe type or namespace that takes
This question is about a general technique in SQL, that I can't quite work
I have a table with two columns, date and score. I want to find
I have been lurking for a long time on this forum and I found
This question exist only because of pure curiosity. Not a homework. Find the fastest
I have a table like this (basic example, not the real thing): FKEY |
Newbie Scala Question: Say I want to do this [Java code] in Scala: public
I have found some in the Cappuccino website (vim, textmate and SubEthaEdit), but not
I have found that my HTML is, to be honest, very clunky. Small, simple
I have found some libraries or web services in PHP that does the job.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.