Unless I explicitly set the primary key to be a value other than
id, I have not been able to get this to work. I have below 2
examples below, one without primary key of id, and one with primary
key of id. Note, if I do turn off validations, the errors below go
away.
I think I may have uncovered somethings that may require
patches, although i am not sure how to implement them. I couldn't
find a forum about this gem, so if there is one, I would like to
know for future questions or discoveries.
I have installed this gem to do mass inserts. I'll start by
listing what does work. I have followed somethings form this post:
http://www.igvita.com/2007/07/11...
The only way I have gotten the import statement to update on
insert is to do the following:
My model
User(facebook_id[primary Key], created_at, updated_at)
this is for a facebook app with rails.
config/environment.rb (bottom of file)
...
...
end
require 'ar-extensions/adapters/mysql'
require 'ar-extensions/import/mysql'
create_table :users, :primary_key => :facebook_id do |t|
t.timestamps
end
class User > ActiveRecord::Base
self.primary_key = "facebook_id"
validates_uniqueness_of :facebook_id
end
When setting the above code, doing the following works as it
seems one should expect at the console:
>>columns=[:facebook_id]
>>values=[[1234],[2345],[3456],[4567]]
>>User.import columns, values, :on_duplicate_key_update => [:updated_at]
=>#<OpenStruct failed_instances=[], num_inserts=1>
>>User.import columns, values, :on_duplicate_key_update => [:updated_at]
=>#<OpenStruct failed_instances=[], num_inserts=1>
but on the other hand, here is my log file:
WARNING: Can't mass-assign these protected attributes: facebook_id
[4;35;1mSQL (0.040990) [0m [0mSELECT `facebook_id` FROM `users` WHERE (`users`.facebook_id IS NULL) [0m
WARNING: Can't mass-assign these protected attributes: facebook_id
[4;36;1mSQL (0.000091) [0m [0;1mSELECT `facebook_id` FROM `users` WHERE (`users`.facebook_id IS NULL) [0m
WARNING: Can't mass-assign these protected attributes: facebook_id
[4;35;1mSQL (0.000086) [0m [0mSELECT `facebook_id` FROM `users` WHERE (`users`.facebook_id IS NULL) [0m
WARNING: Can't mass-assign these protected attributes: facebook_id
[4;36;1mSQL (0.000056) [0m [0;1mSELECT `facebook_id` FROM `users` WHERE (`users`.facebook_id IS NULL) [0m
WARNING: Can't mass-assign these protected attributes: facebook_id
[4;35;1mSQL (0.000054) [0m [0mSELECT `facebook_id` FROM `users` WHERE (`users`.facebook_id IS NULL) [0m
[4;36;1mSQL (0.000252) [0m [0;1mSHOW VARIABLES like 'max_allowed_packet'; [0m
[4;35;1mClass Create Many Without Validations Or Callbacks (0.017332) [0m [0mINSERT INTO `users` (`facebook_id`,`created_at`,`updated_at`) VALUES (1153003682,'2008-11-12 01:51:23','2008-11-12 01:51:23'),(1159273529,'2008-11-12 01:51:23','2008-11-12 01:51:23'),(1209008725,'2008-11-12 01:51:23','2008-11-12 01:51:23'),(1212122770,'2008-11-12 01:51:23','2008-11-12 01:51:23'),(1231862724,'2008-11-12 01:51:23','2008-11-12 01:51:23') ON DUPLICATE KEY UPDATE `users`.`updated_at`=VALUES(`updated_at`),`users`.`updated_at`=VALUES(`updated_at`) [0m
I see warnings, and 'create many without validations or
callbacks'. Even if I explcitly call :validate => true and pass
that in as option, I still receive the create without validations
or callbacks. Not sure why.
Here is what does not work. I can't get the above code to work
when an auto increment id is the primary key of User.
My model
User(id, facebook_id, created_at, updated_at)
Here is my schema:
create_table :users do |t|
t.column :facebook_id, :null => false
t.timestamps
end
add_index "users", :facebook_id, :unique=>true
class Users > ActiveRecord::Base
validates_uniqueness_of :facebook_id
end
Here is what I am trying to do at the console:
>>columns=[:facebook_id]
>>values=[[1234],[2345],[3456],[4567]]
>>User.import columns, values, :on_duplicate_key_update => [:updated_at]
=>#<OpenStruct failed_instances=[], num_inserts=1>
>>User.import columns, values, :on_duplicate_key_update => [:updated_at]
=> #<OpenStruct failed_instances=[#<User id: nil, facebook_id: 1153003682, created_at: "2008-11-12 02:05:05", updated_at: "2008-11-12 02:05:05">, #<User id: nil, facebook_id: 1159273529, created_at: "2008-11-12 02:05:05", updated_at: "2008-11-12 02:05:05">, #<User id: nil, facebook_id: 1209008725, created_at: "2008-11-12 02:05:05", updated_at: "2008-11-12 02:05:05">, #<User id: nil, facebook_id: 1212122770, created_at: "2008-11-12 02:05:05", updated_at: "2008-11-12 02:05:05">, #<User id: nil, facebook_id: 1231862724, created_at: "2008-11-12 02:05:05", updated_at: "2008-11-12 02:05:05">], num_inserts=0>
My thinking is that this would update all the updated_at columns
everytime a duplicate entry is being inserted. For some reason, I
get failed attempts everytime this happens.
Here is my development.log output after a first insert:
[4;35;1mUser Columns (0.001432) [0m [0mSHOW FIELDS FROM `users` [0m
[4;36;1mSQL (0.000524) [0m [0;1mSELECT `facebook_id` FROM `users` WHERE (`users`.facebook_id = 1153003682) [0m
[4;35;1mSQL (0.000318) [0m [0mSELECT `facebook_id` FROM `users` WHERE (`users`.facebook_id = 1159273529) [0m
[4;36;1mSQL (0.000286) [0m [0;1mSELECT `facebook_id` FROM `users` WHERE (`users`.facebook_id = 1209008725) [0m
[4;35;1mSQL (0.000367) [0m [0mSELECT `facebook_id` FROM `users` WHERE (`users`.facebook_id = 1212122770) [0m
[4;36;1mSQL (0.000451) [0m [0;1mSELECT `facebook_id` FROM `users` WHERE (`users`.facebook_id = 1231862724) [0m
[4;35;1mSQL (0.000482) [0m [0mSHOW VARIABLES like 'max_allowed_packet'; [0m
[4;36;1mClass Create Many Without Validations Or Callbacks (0.030564) [0m [0;1mINSERT INTO `users` (`facebook_id`,`created_at`,`updated_at`) VALUES (1153003682,'2008-11-12 01:20:50','2008-11-12 01:20:50'),(1159273529,'2008-11-12 01:20:50','2008-11-12 01:20:50'),(1209008725,'2008-11-12 01:20:50','2008-11-12 01:20:50'),(1212122770,'2008-11-12 01:20:50','2008-11-12 01:20:50'),(1231862724,'2008-11-12 01:20:50','2008-11-12 01:20:50') ON DUPLICATE KEY UPDATE `users`.`updated_at`=VALUES(`updated_at`),`users`.`updated_at`=VALUES(`updated_at`) [0m
The above says 'create many without validations or callbacks',
not sure why, as the docs say that validations are on by default.
And here is my development.log file after a second import with the
same exact data:
[4;36;1mSQL (0.000840) [0m [0;1mSELECT `facebook_id` FROM `users` WHERE (`users`.facebook_id = 1153003682) [0m
[4;35;1mSQL (0.000197) [0m [0mSELECT `facebook_id` FROM `users` WHERE (`users`.facebook_id = 1159273529) [0m
[4;36;1mSQL (0.000144) [0m [0;1mSELECT `facebook_id` FROM `users` WHERE (`users`.facebook_id = 1209008725) [0m
[4;35;1mSQL (0.000140) [0m [0mSELECT `facebook_id` FROM `users` WHERE (`users`.facebook_id = 1212122770) [0m
[4;36;1mSQL (0.000137) [0m [0;1mSELECT `facebook_id` FROM `users` WHERE (`users`.facebook_id = 1231862724) [0m
As you can see, nothing is happening, no inserts or updates are
made. I have no idea why this is happening. I am on
ar-extensions-0.8.0 and Rails 2.1.2.
What am I missing? I have spent about 2 days on this can can't
find anything online. I would like this to work without getting
warnings, and using an id as the primary key.
Any help or guidance on how to get this working would be greatly
appreciated.