Reputation: 36
I meet few problems with ahoy visit model in my rails app
I have a lot of
Mysql2::Error: Duplicate entry '$$' for key 'PRIMARY': INSERT INTO `visits`
where $$ is a number
when I print last records
Visits=Visit.find(:all, :order => "started_at desc", :limit => 5)
Visits.each do |visit| puts "#{visit.id} -- #{visit.started_at}" end
display is
99904352 -- 2018-02-01 11:32:51 UTC
8918 -- 2018-02-01 09:59:04 UTC
90866 -- 2018-02-01 09:09:10 UTC
99904351 -- 2018-02-01 09:03:00 UTC
99904350 -- 2018-02-01 08:58:24 UTC
I have already try to reset autoincrement value
ALTER TABLE visits AUTO_INCREMENT = max_value+1;
without success ..
any ideas ? I would like to avoid dump/drop/reload DB
thanks !
Upvotes: 1
Views: 391
Reputation: 36
ok I had a false before_create that try to set id with uuid instead of integer .. I have comment it .. I have to wait but I believe that was my problem ..
my bad :p
Upvotes: 0
Reputation: 3005
When you find a duplicate key error, such as Mysql2::Error: Duplicate entry '$$' for key 'PRIMARY': INSERT INTO visits
, you should check two things:
You must of course change your program to avoid inserting records setting the key mannually, and you must update the autoincrement value, to the next available one. To do this, search for the largest value and set it to the next one.
Select max(id) from visits;
ALTER TABLE visits AUTO_INCREMENT = max_value + 1;
Upvotes: 3