# Ar-extensions import - on\_duplicate\_key\_update error

**URL:** <https://www.ruby-forum.com/t/ar-extensions-import-on-duplicate-key-update-error/146534>\
**Category:** Rails\
**Created:** [August 22, 2008, 3:52am UTC](https://www.ruby-forum.com/t/ar-extensions-import-on-duplicate-key-update-error/146534 "2008-08-22T03:52:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![frexuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b9bd4f/32.png) [@frexuz](https://www.ruby-forum.com/u/frexuz)\
**Post date:** [August 22, 2008, 3:52am UTC](https://www.ruby-forum.com/t/ar-extensions-import-on-duplicate-key-update-error/146534/1 "2008-08-22T03:52:00Z")

</div>

This works perfectly the first time i run the script and every row gets  
inserted.  
But when ran a second time, when the code should update duplicate table  
rows instead of inserting, then this error appears (first object in the  
array):

`Mysql::Error: #23000Duplicate entry 'One Tree Hill' for key
'show_must_be_unique'`

Lets go through the code real quick…

[code]class CreateShows \< ActiveRecord::Migration  
def self.up  
create\_table :shows do |t|  
t.string :name  
t.string :url  
t.string :genre  
t.datetime :start\_year  
t.datetime :end\_year

```
  t.timestamps
end
            add_index(:shows, [:name], :unique => true, :name =>

```

“show\_must\_be\_unique”)  
end

def self.down  
drop\_table :shows  
end  
end[/code]

[quote] require “ar-extensions”  
require “hpricot”  
require “open-uri”

def index

```
 @columns = [
   :name,
   :url,
   :genre,
   :start_year,
   :end_year
 ]
 @values = scrape_shows() #returns a correct array with Show objects
 @options = {:validate => false, :on_duplicate_key_update => [:url,

```

:start\_year, :end\_year]}

```
 Show.import @columns, @values, @options
 @values.clear

 @shows = Show.find(:all)
 respond_to do |format|
   format.html #index.html.erb
 end

```

end  
[/quote]

I’ve google around alot for any solution with no luck.

My friend introduced me to AR-extensions and he simply gave me this  
code, which run fine for him, then of course we don’t have the same  
setup.

Anyone have experience with AR-extensions on Windows?  
I’m running:  
Ruby 1.8.6  
Rails 2.1.0  
Mongrel 1.1.5  
AR-extensions 0.8.0  
(yes, with MySQL)

Somehow the code won’t recognize a duplicate and still tries to do an  
INSERT instead of an UPDATE.
