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

taggit.models.TaggedItem.DoesNotExist in PostgreSQL

Open sowdust opened this issue 4 years ago • 8 comments

Hello, I have deployed the same application both locally with a SQLite db and on a production server using apache and PostgreSQL. I have a piece of code that adds tags to objects (I have added some logger.debug calls for debugging):

def tag_add(request):
    object_id = request.POST.get('object_id')
    object_type = request.POST.get('object_type')
    if object_type not in taggable_types:
        return _error('Object type not taggable')
    taggable_object = _get_taggable_object(object_type, object_id)
    tag = request.POST.get('tag_name', None).lower()
    tags = taggable_object.tags.names()
    logger.debug(taggable_object)
    logger.debug(tags)
    logger.debug(tag)
    if tag not in tags:
        logger.debug('inserting')
        taggable_object.tags.add(tag)
        logger.debug('inserted')

The above piece of code works perfectly on SQLite. When moving to PostgreSQL, however, I get the error taggit.models.TaggedItem.DoesNotExist: TaggedItem matching query does not exist.

The object exists, as it can be seen by the below stack trace, where it is correctly printed by the above debugging instructions. It is also taggable, as the following is the first line of the class declaration:

class TwitterUser(models.Model):
    tags = TaggableManager()

Stacktrace:

 <Object name>
 <QuerySet []>
 tag content 
 inserting
 Internal Server Error: /ajax/tag/add/
 Traceback (most recent call last):
   File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 573, in get_or_create
     return self.get(**kwargs), False
   File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 431, in get
     self.model._meta.object_name
 taggit.models.TaggedItem.DoesNotExist: TaggedItem matching query does not exist.
 
 During handling of the above exception, another exception occurred:
 
 Traceback (most recent call last):
   File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 84, in _execute
     return self.cursor.execute(sql, params)
 psycopg2.DataError: integer out of range
 
 
 The above exception was the direct cause of the following exception:
 
 Traceback (most recent call last):
   File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py", line 47, in inner
     response = get_response(request)
   File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 179, in _get_response
     response = wrapped_callback(request, *callback_args, **callback_kwargs)
   File "/usr/local/lib/python3.6/dist-packages/django/views/decorators/http.py", line 40, in inner
     return func(request, *args, **kwargs)
   File "/tafferugli/twitter/views.py", line 216, in wrapper
     return view_func(request, *args, **kwargs)
   File "/tafferugli/twitter/views.py", line 1148, in tag_add
     taggable_object.tags.add(tag)
   File "/usr/local/lib/python3.6/dist-packages/taggit/utils.py", line 124, in inner
     return func(self, *args, **kwargs)
   File "/usr/local/lib/python3.6/dist-packages/taggit/managers.py", line 176, in add
     tag=tag, **self._lookup_kwargs()
   File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 576, in get_or_create
     return self._create_object_from_params(kwargs, params)
   File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 610, inreate_object_from_params
     obj = self.create(**params)
   File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 447, in create
     obj.save(force_insert=True, using=self.db)
   File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py", line 751, in save
     force_update=force_update, update_fields=update_fields)
   File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py", line 789, in save_base
     force_update, using, update_fields,
   File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py", line 892, in _save_table
     results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
   File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py", line 932, in _do_insert
     using=using, raw=raw,
   File "/usr/local/lib/python3.6/dist-packages/django/db/models/manager.py", line 85, in manager_method
     return getattr(self.get_queryset(), name)(*args, **kwargs)
   File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 1249, in _insert
     return query.get_compiler(using=using).execute_sql(returning_fields)
   File "/usr/local/lib/python3.6/dist-packages/django/db/models/sql/compiler.py", line 1395, in execute_sql
     cursor.execute(sql, params)
   File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 66, in execute
     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
   File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
     return executor(sql, params, many, context)
   File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 84, in _execute
     return self.cursor.execute(sql, params)
   File "/usr/local/lib/python3.6/dist-packages/django/db/utils.py", line 90, in __exit__
     raise dj_exc_value.with_traceback(traceback) from exc_value
   File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 84, in _execute
     return self.cursor.execute(sql, params)
 django.db.utils.DataError: integer out of range

sowdust avatar Sep 25 '20 10:09 sowdust

I had the same problem when I used the UUID field for the primary key. if you use the UUID field when creating a model, that contains a tag field. Replace the primary key with an integer

Gutei avatar Sep 30 '20 10:09 Gutei

Thanks for the suggestion, but unluckily that should not be the problem, since the class TwitterUser has an integer as primary key:

class TwitterUser(models.Model):
    tags = TaggableManager()
    # ...
    id_int = models.BigIntegerField(primary_key=True)

sowdust avatar Sep 30 '20 12:09 sowdust

Thanks for the suggestion, but unluckily that should not be the problem, since the class TwitterUser has an integer as primary key:

class TwitterUser(models.Model):
    tags = TaggableManager()
    # ...
    id_int = models.BigIntegerField(primary_key=True)

the problem here image

I suggest you not set the primary key, use Django to support for that, and don't forget to migrate

Gutei avatar Sep 30 '20 13:09 Gutei

I can't do that : ( I need a BigInteger and I need it be called id_int. I think this line should be changed to allow bigger integers: https://github.com/jazzband/django-taggit/blob/master/taggit/models.py#L160

sowdust avatar Sep 30 '20 13:09 sowdust

I confirm that using BigIntegerField solves the problem https://github.com/jazzband/django-taggit/pull/693

sowdust avatar Sep 30 '20 13:09 sowdust

this can be solved by using a Custom Tag (or rather, a custom through model). With custom tags you have loads of flexibility on your data store, so you don't end up with these kinds of nasty issues.

rtpg avatar Apr 14 '21 07:04 rtpg

I think that solves.

https://django-taggit.readthedocs.io/en/latest/custom_tagging.html#genericuuidtaggeditembase

mjr avatar May 05 '21 01:05 mjr

I confirm that using BigIntegerField solves the problem #693

I am using BigIntField as a primary key but getting the error..

n4ff4h avatar Feb 02 '23 13:02 n4ff4h