From f0e155c77fef448a2362a3cef7d5afe7c979fc5b Mon Sep 17 00:00:00 2001 From: Yan Date: Tue, 17 Oct 2017 17:03:27 +0630 Subject: [PATCH] fix duplicate key in seed --- app/models/seed_generator.rb | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/app/models/seed_generator.rb b/app/models/seed_generator.rb index a5bf5976..a174b904 100644 --- a/app/models/seed_generator.rb +++ b/app/models/seed_generator.rb @@ -2,6 +2,7 @@ class SeedGenerator < ApplicationRecord # Generate ID for Tables def self.generate_id(model, prefix) seed = SeedGenerator.find_by_model(model) + currentNo = seed.current new_receipt_no = 0 if (seed.nil?) @@ -9,16 +10,17 @@ class SeedGenerator < ApplicationRecord seed.model = model new_receipt_no = seed.next seed.save - - else - current_no = seed.next - seed.next = seed.next + seed.increase_by - seed.current = current_no - seed.save + else + next_current = self.update_seed(model, seed.next, seed.increase_by) + currentNo = next_current + + # current_no = seed.next + # seed.next = seed.next + seed.increase_by + # seed.current = current_no + # seed.save end - padding_len = 15 - prefix.length - saleOrderId = prefix +"-"+ seed.current.to_s.to_s.rjust((14-prefix.length)+1,'0') + saleOrderId = prefix +"-"+ currentNo.to_s.to_s.rjust((14-prefix.length)+1,'0') return saleOrderId end @@ -71,4 +73,22 @@ class SeedGenerator < ApplicationRecord next_code = prefix + seed.current.to_s.to_s.rjust((count)+1,'0') return next_code end + + private + def self.update_seed(model, current, inc) + next_val = 0 + nex = current + inc + update_sql = "update seed_generators set current= #{current}, next= #{nex} where model='#{model}';"; + select_sql = "select * from seed_generators where model='#{model}';" + update_result = ActiveRecord::Base.connection.execute(update_sql); + Rails.logger.debug "SeedGenerator -> " + update_result.to_s + + select_result = ActiveRecord::Base.connection.execute(select_sql); + select_result.each do |row| + p row[3] + next_val = row[3] + end + + return next_val + end end