Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

taggit.models.TaggedItem.DoesNotExist in PostgreSQL #692

Open
sowdust opened this issue Sep 25, 2020 · 8 comments
Open

taggit.models.TaggedItem.DoesNotExist in PostgreSQL #692

sowdust opened this issue Sep 25, 2020 · 8 comments
Labels

Comments

@sowdust
Copy link

sowdust commented Sep 25, 2020

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

@Gutei
Copy link

Gutei commented Sep 30, 2020

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

@sowdust
Copy link
Author

sowdust commented Sep 30, 2020

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)

@Gutei
Copy link

Gutei commented Sep 30, 2020

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

@sowdust
Copy link
Author

sowdust commented Sep 30, 2020

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
Copy link
Author

sowdust commented Sep 30, 2020

I confirm that using BigIntegerField solves the problem #693

@rtpg
Copy link
Contributor

rtpg commented Apr 14, 2021

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 rtpg added the question label Apr 14, 2021
@mjr
Copy link

mjr commented May 5, 2021

I think that solves.

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

@n4ff4h
Copy link

n4ff4h commented Feb 2, 2023

I confirm that using BigIntegerField solves the problem #693

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants