Adriano Nagel
2009-Aug-26 14:56 UTC
Strange Active::Record behavior, StatementInvalid exception thrown twice?
Hi, I''m a bit puzzled with an ActiveRecord::StatementInvalid exception that''s apparently being thrown twice. I''m using Rails 2.3.3 and Postgres, and inside the Feed Model I have something like: has_many :entries, :dependent => :delete_all def process_feed ... process_feed_entries # log saving feed save! # will fail if an exception is thrown below end def process_feed_entries ... begin # log saving entry entry.save! # can fail because of duplicate key values rescue ActiveRecord::StatementInvalid => e # log exception caught # log the error in the db (Log Model) end end DB log: * Saving entry BEGIN PGError: ERROR: duplicate key value ... : INSERT INTO "entries" ... ROLLBACK * Exception caught BEGIN INSERT INTO "logs" ... COMMIT * Saving feed BEGIN UPDATE "feeds" ... PGError: ERROR: duplicate key value ... : INSERT INTO "entries" ROLLBACK ActiveRecord::StatementInvalid: PGError: ERROR: duplicate key ... Stack trace The last exception that terminates execution was (or should have been) caught inside the begin/rescue block. Why is it thrown again when the feed is saved, if the transaction was rolled back? Any hints much appreciated! Thanks, -- Adriano
Adriano Nagel
2009-Aug-26 16:49 UTC
Re: Strange Active::Record behavior, StatementInvalid exception thrown twice?
On Aug 26, 11:56 am, Adriano Nagel <a...-pZDOhbmk9TnQT0dZR+AlfA@public.gmane.org> wrote:> has_many :entries, :dependent => :delete_all > save! # will fail if an exception is thrown belowNothing like explaining your problem to others :-) I figure feed.save tries to save the associated entry and that throws the exception again. I have to delete the offending entry inside the rescue clause. Thanks, -- Adriano
Frederick Cheung
2009-Aug-26 18:39 UTC
Re: Strange Active::Record behavior, StatementInvalid exception thrown twice?
On Aug 26, 3:56 pm, Adriano Nagel <a...-pZDOhbmk9TnQT0dZR+AlfA@public.gmane.org> wrote:> Hi, > > I''m a bit puzzled with an ActiveRecord::StatementInvalid exception > that''s apparently being thrown twice. > > I''m using Rails 2.3.3 and Postgres, and inside the Feed Model I have > something like: > > has_many :entries, :dependent => :delete_all > > def process_feed > ... > process_feed_entries > # log saving feed > save! # will fail if an exception is thrown below > end > > def process_feed_entries > ... > begin > # log saving entry > entry.save! # can fail because of duplicate key valuesIs entry a member of the entries collection ? If so then the docs on associations state "All unsaved (new_record? == true) members of the collection are automatically saved when the parent is saved." ie when you call feed.save! unsaved association members are saved. Fred> rescue ActiveRecord::StatementInvalid => e > # log exception caught > # log the error in the db (Log Model) > end > end > > DB log: > > * Saving entry > BEGIN > PGError: ERROR: duplicate key value ... : INSERT INTO "entries" ... > ROLLBACK > * Exception caught > BEGIN > INSERT INTO "logs" ... > COMMIT > * Saving feed > BEGIN > UPDATE "feeds" ... > PGError: ERROR: duplicate key value ... : INSERT INTO "entries" > ROLLBACK > ActiveRecord::StatementInvalid: PGError: ERROR: duplicate key ... > Stack trace > > The last exception that terminates execution was (or should have been) > caught inside the begin/rescue block. Why is it thrown again when the > feed is saved, if the transaction was rolled back? > > Any hints much appreciated! > > Thanks, > > -- > Adriano
Frederick Cheung
2009-Aug-26 18:40 UTC
Re: Strange Active::Record behavior, StatementInvalid exception thrown twice?
On Aug 26, 7:39 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 26, 3:56 pm, Adriano Nagel <a...-pZDOhbmk9TnQT0dZR+AlfA@public.gmane.org> wrote: > > Is entry a member of the entries collection ? If so then the docs on > associations state > >Oops, you beat me to it ! Fred