On Sunday, August 30, 2015 at 12:18:50 AM UTC-7, Janko Marohnić wrote:
>
> Through some debugging I found out that it will happen if you forget to 
> initialize the counter cache column to 0 (it doesn't have anything to do 
> with multi_insert). The below example reproduces the issue, but if I write 
> "Integer :questions_count, default: 0" in the below example, it will work.
>
> require "sequel"
> require "sequel_postgresql_triggers"
>
> system "createdb counter_cache_bug"
> DB = Sequel.connect("postgres:///counter_cache_bug")
> at_exit do
>   DB.disconnect
>   system "dropdb counter_cache_bug"
> end
>
> DB.create_table :quizzes do
>   primary_key :id
>   Integer :questions_count
> end
> DB.create_table :questions do
>   primary_key :id
>   foreign_key :quiz_id, :quizzes
> end
> DB.pgt_counter_cache(:quizzes, :id, :questions_count, :questions, :quiz_id
> )
>
> DB[:quizzes].insert({})
> DB[:questions].insert({quiz_id: DB[:quizzes].first[:id]})
>
> p DB[:quizzes].first[:questions_count]
>
> It's probably not a bug, though, you should have the column initialized to 
> 0, although it would be nice to have sequel_postgresql_triggers do it for 
> you if you forgot (like ActiveRecord's counter cache does, and probably 
> also Sequel::Model's counter cache).
>

It's not a code bug, but it is a documentation bug that the requirement of 
a 0 default is not documented. I've pushed a patch to 
sequel_postgresql_triggers to fix that.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to