jazzband / django-taggit

Simple tagging for django
https://django-taggit.readthedocs.io
BSD 3-Clause "New" or "Revised" License
3.27k stars 623 forks source link

Excluding tags in Model Managers causes failures #857

Open adinhodovic opened 1 year ago

adinhodovic commented 1 year ago

Hmm... Weird one adding qs.exclude(tags__name="test) to our object manager causes errors on a fresh database

My model and manager:

class ActiveModelManager(models.Manager):
    def get_queryset(self):
        qs = super().get_queryset()
        qs = qs.exclude(
            tags__name="test",
        )
        return qs

class TestModel(TimeStampedModel):

    tags = TaggableManager(blank=True, verbose_name="Tags")

    # Exclude inactive providers
    objects = ActiveModelManager()  # type: ignore
    # Include inactive providers
    all_objects = models.Manager()  # type: ignore

Running ./manage.py migrate causes an exception:

Traceback (most recent call last):
  File ".../.venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1335, in build_filter
    join_info = self.setup_joins(
  File ".../.venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1648, in setup_joins
    path, final_field, targets, rest = self.names_to_path(
  File ".../.venv/lib/python3.10/site-packages/django/db/models/sql/query.py", line 1581, in names_to_path
    raise MultiJoin(pos + 1, names_with_path)
django.db.models.sql.datastructures.MultiJoin: (1, [('tags', [PathInfo(from_opts=<Options for <My_Model>, to_opts=<Options for TaggedItem>, target_fields=[<hashid_field.field.BigHas
hidAutoField: id>], join_field=<ManyToManyRel: <my_app>.<my_model>, m2m=True, direct=False, filtered_relation=None)])])

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../.venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "django_content_type" does not exist
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...

This issue occurs on fresh database such as in CI, adding it to a database that's already done migrations works fine. Any ideas, can't figure it out?

rtpg commented 11 months ago

(Sorry for the delay here) I think you need to make sure to have run migrations? Tags rely on the content type table and the like to implement its generic foreign key stuff. I don't think any Django stuff really supports being run on a "fresh DB" without running migrations, but I might be misunderstanding what you are saying