1

我每天运行 ruby​​ 脚本作业,用于将数据从一个表加载到另一个表,直到在源表中找到重复的键记录,因为该表没有约束。我的目标表拒绝了这些记录,因为目标表对列(head_date、center_id、site_url)有主键约束。我的程序刚刚停止并在命令行上显示以下错误。:-(

in `async_exec': PG::Error: ERROR:  duplicate key value violates unique constraint "tst_data_pkey" (ActiveRecord::RecordNotUnique)

我可以期望源表有其他像这样的讨厌的记录。捕获此活动记录异常后,如何继续处理并移至下一条记录?

以下是我的更新/插入的代码:

class SiteSection < ActiveRecord::Base

  SiteSection.establish_connection(
        :adapter  => 'postgresql',
        :host     => 'hoster-of-hosts.com',
        :database => 'super-inven',
        :username => 'user',
        :password => 'pass'
                      )
  self.table_name = 'master.target_tbl'  # << insert into this table
end

conn.query("select * from source_tbl") do |row|
  siteData = SiteSection.find_or_initialize_by_head_date_and_center_id_and_site_url(row[:date_c], row[:com_id], row[:site_link])

  siteData[:head_date]      = row[:date_c]
  siteData[:center_id]     = row[:com_id]
  siteData[:site_url]          = row[:site_link].nil? ? 'unknown' : row[:site_link]
  siteData[:people_cnt]      = row[:persons].nil? ? 0 : row[:persons]
  siteData[:ips]     = row[:ip_adds].nil? ? 0 : row[:ip_adds]

  siteData.save
i = i+1
puts "finished: #{i}" if i % 10000 == 0
end
conn.close
4

2 回答 2

3

你可以使用一个

begin

rescue => e

end

像这样:

class SiteSection < ActiveRecord::Base

  SiteSection.establish_connection(
        :adapter  => 'postgresql',
        :host     => 'hoster-of-hosts.com',
        :database => 'super-inven',
        :username => 'user',
        :password => 'pass'
                      )
  self.table_name = 'master.target_tbl'  # << insert into this table
end

conn.query("select * from source_tbl") do |row|
  siteData = SiteSection.find_or_initialize_by_head_date_and_center_id_and_site_url(row[:date_c], row[:com_id], row[:site_link])

  siteData[:head_date]      = row[:date_c]
  siteData[:center_id]     = row[:com_id]
  siteData[:site_url]          = row[:site_link].nil? ? 'unknown' : row[:site_link]
  siteData[:people_cnt]      = row[:persons].nil? ? 0 : row[:persons]
  siteData[:ips]     = row[:ip_adds].nil? ? 0 : row[:ip_adds]

  begin
    siteData.save
  rescue => e
    puts e.message
    puts "Error happened but I'll just keep chuggin along"
  end
i = i+1
puts "finished: #{i}" if i % 10000 == 0
end
conn.close

rescue => e期望捕获错误。它会通过不让它冒泡来吞下这个错误。您的代码将继续运行,而不会出现异常崩溃。

于 2013-03-13T03:21:24.167 回答
-1

如果你有唯一的密钥,你应该使用它——像这样:

i = 0
puts 'loading records'
conn.query("select * from source_tbl") do |row|
  rec = SiteSection.where(
    :head_date => row[:date_c],
    :center_id => row[:com_id],
    :site_url => (row[:site_link] || 'unknown')
  ).first_or_initialize

  rec.people_cnt = (row[:persons] || 0)
  rec.ips => (row[:ip_adds] || 0)

  rec.save!

  i += 1
  print '.' if i % 10000 == 0
end
puts "done\ntotal records: #{i}"

这样,您将保存记录(新)或更新记录(如果找到)。ActiveRecord::RecordNotUnique如果你没有改变你的 pkey,不应该在这里!

于 2013-03-18T21:16:10.263 回答