开发者

ActiveRecord not saving foreign key? Really weird?

开发者 https://www.devze.com 2023-03-27 09:26 出处:网络
Firstly, sorry about the essay, I\'m trying not to provide any actual application code and hope there is some logical explanation just because the problem is so weird.

Firstly, sorry about the essay, I'm trying not to provide any actual application code and hope there is some logical explanation just because the problem is so weird.

I have spent the past 2 days debugging some code that works 95% of the time, the error seemed simple to debug at first but now I find myself scratching my head with no answer(s).

Some background

We are running: Ruby 1.8.6, Rails 2.3.2 & Postgresql 8

We still have to migrate to 2.3.8, so unless the solution lies within upgrading to rails 2.3.8 don't advise me on it ;)

In a nutshell

We have a script that scans through CSV files. I store all the relevant columns into hashes by row and then proceed to iterate through the hashes afterwards, calling various ActiveRecord models and methods to store the data into our database.

The problem

The most important data that needs to get saved have method calls that are wrapped inside a Transaction block, so if any errors are raised, no data should get inserted into our core tables.

The table in question has 2 foreign keys that BOTH need to be present in order for the rails application to function as expected.

Like I said, ~95% of the time while processing our data, these values get inserted correctly.

The other ~5% of the time the one foreign key value does not get saved at all, and for no good reason.

I have saved the script flow and object/variable output into a log file and stepped through/scrutinised the log file for the instances where the foreign key was missing.

The objects/variables that I use as foreign keys were all reported as 'saved' by ActiveRecord in each instance where I inspected the objects after being 'saved'.

One thing that might be worth noting is that the value that gets saved into the foreign key column in question gets computed outside of the Transaction block - but I don't see why that would be a problem, seeing as I can output and use the value further down the line.

Simplified code flow

#Returns ActiveRecord object
fk2_source_object = method_to_compute_fk2(x, y)
@logger.info fk2_source_object.inspect

begin
  Transaction do
    begin
      fk1_source_object = FK1Model.new
      #etc, etc.
      fk1_source_object.save!
      @logger.info fk1_source_object.inspect

      object_in_question = ObjectInQuestion.new
      object_in_question.fk1_source_object_id = fk1_source_object.id

      #FK2 >> This is the value that does not reflect in the database!? even if i could inspect it and see an id after calling .save!.
      object_in_question.fk2_source_object_id = fk2_source_object.id

      begin
        object_in_question.save!
        开发者_高级运维#At this point it shows that the object has saved, all values have been set, but it does not reflect in the database?
        @logger.info object_in_question.inspect
      rescue
        @Logger.error "error message"
        raise ActiveRecord::Rollback
      end
    rescue
      @Logger.error "error message"
      raise ActiveRecord::Rollback
    end
  end
rescue Exception => e
  @logger.error e.inspect
end
#etc, etc.

I'm currently grasping at straws, going to wrap the entire section into the Transaction block.

I cannot recreate the error on my development box, quite frankly if i rerun the csv files on the server, the values get inserted correctly the second / third time. (So it's not a data source problem)

I'm starting to worry that it may be a rails / postgresql 8 problem? Am I losing the plot or what can be the causes of this?

0

精彩评论

暂无评论...
验证码 换一张
取 消