django-hitcount icon indicating copy to clipboard operation
django-hitcount copied to clipboard

MultipleObjectsReturned at [...] get() returned more than one HitCount -- it returned 2!

Open siovene opened this issue 4 years ago • 2 comments

I run hitcount 1.2.3 on Django 1.11.28

To register a hit, I use the following code, as per the docs:

<script src="{% static 'hitcount/jquery.postcsrf.js' %}"></script>

{% get_hit_count_js_variables for image as hitcount %}
<script type="text/javascript">
jQuery(document).ready(function($) {
    // use the template tags in our JavaScript call
    $.postCSRF("{{ hitcount.ajax_url }}", { hitcountPK : "{{ hitcount.pk }}" })
        .done(function(data) {
            var $el = $('.views .number');
            var views = parseInt($el.text());
            if (data.hit_counted) {
                $el.text(views + 1);
            }
        }).fail(function(data) {
            console.log('POST failed');
            console.log(data);
        });
});
</script>

However, every now and then, when I try to get the hit count on an object, I get the error in the title of this issue.

Sounds like there's a race condition and two HitCounts are created at the same time.

Are you aware of this? Is this an issue with django-hitcount or my code?

Thanks!

siovene avatar Mar 08 '20 20:03 siovene

Are you aware of this? Is this an issue with django-hitcount or my code?

how can I reproduce it?

bashu avatar Jul 04 '20 09:07 bashu

I'm sorry, @bashu, I do not have a minimal reproduction repository or anything. But I do use it as per the documentation. This is definitely an issue with concurrent hits happening at the same time, so there should be a locking mechanism in place to prevent this.

siovene avatar Jul 06 '20 08:07 siovene