Reputation: 2176
I have the following piece of code which copies data from an image table in database A (shard_A) to another table in database B (shard_B)
Here is the structure of the image table in both databases
Image table (shard_A) - Image table (shard_B)
- image_id - image_id
- submission_fk - sub_fk
- image_desc - img_desc
The column names of the 2 tables are slightly different
@test_shard_A = Image.using(:shard_A).find(:first, :conditions => ["submission_fk =?", @sub.id ])
Octopus.using(:shard_B) do
Image.create(
:image_id => @test_shard_A.image_id,
:sub_fk => @test_shard_A.submission_fk,
:img_desc => @test_shard_A.image_desc
) do |primary|
primary.img_id = @test_shard_A.image_id
end
end
The above is giving me the following error message:
unknown attribute: sub_fk
What am I doing wrong?
Upvotes: 0
Views: 560
Reputation: 5668
ActiveRecord loads metadata (table information) when the first query is executed for a table. When you start your application with a query to shard_A, it checks the table structure, and constructs your Image class according to it, with submission_fk.
When you change shards, it won't check the table structure again, so it assumes that there is a submission_fk, and assumes no sub_fk, since it doesn't know about that.
I suggest you to use the same structure for all shards, but if you can't do that, you may force ActiveRecord to reload the table information - I never used Octopus, but I think it will work:
Image.reset_column_information
Note that it will degrade the performance of the application, since reloading the metadata is a slow operation.
Upvotes: 2