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).
On Sunday, August 30, 2015 at 4:44:37 AM UTC+2, Jeremy Evans wrote:
>
> On Saturday, August 29, 2015 at 4:49:06 PM UTC-7, Janko Marohnić wrote:
>>
>> Jeremy, I wanted to ask you something about these triggers. I use the
>> counter cache trigger, but if I multi_insert the objects that are counted,
>> the counter caches don't get updated. Is this Postgres' limitation? Could
>> this also happen on dataset-level updates, that the trigger isn't hit?
>>
>
> The counter caches should get updated correctly, regardless of whether you
> are inserting multiple rows at once or a single row. Unless you've
> disabled triggers, if the counter cache isn't being updated correctly,
> that's probably either a bug or a misconfiguration. Can you post a
> self-contained example that I can test with?
>
> 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.