wlmac / metropolis

William Lyon Mackenzie's online hub for announcements, calendar events, clubs, and timetables
https://maclyonsden.com
GNU Affero General Public License v3.0
21 stars 7 forks source link

Listing filters return nothing on tuplicate fields. #211

Closed JasonLovesDoggo closed 1 year ago

JasonLovesDoggo commented 1 year ago

When doing a request such as /api/v3/obj/announcement?tags=10&tags=42&search_type=AND

the SQL is

SELECT DISTINCT "core_announcement"."id",
                "core_announcement"."author_id",
                "core_announcement"."created_date",
                "core_announcement"."last_modified_date",
                "core_announcement"."show_after",
                "core_announcement"."title",
                "core_announcement"."body",
                "core_announcement"."organization_id",
                "core_announcement"."is_public",
                "core_announcement"."supervisor_id",
                "core_announcement"."status",
                "core_announcement"."rejection_reason"
FROM "core_announcement"
         INNER JOIN "core_organization" ON ("core_announcement"."organization_id" = "core_organization"."id")
         LEFT OUTER JOIN "core_user_organizations"
                         ON ("core_organization"."id" = "core_user_organizations"."organization_id")
         LEFT OUTER JOIN "core_organization_execs"
                         ON ("core_organization"."id" = "core_organization_execs"."organization_id")
         INNER JOIN "core_announcement_tags" ON ("core_announcement"."id" = "core_announcement_tags"."announcement_id")
WHERE ((("core_announcement"."status" = 'a' AND "core_announcement"."is_public") OR
        ("core_announcement"."status" = 'a' AND "core_user_organizations"."user_id" = 3) OR
        "core_organization_execs"."user_id" IN (3)) AND
       ("core_announcement_tags"."tag_id" = 10 AND "core_announcement_tags"."tag_id" = 42))
ORDER BY "core_announcement"."show_after" DESC

vs something along the lines of

SELECT DISTINCT "core_announcement"."id",
                "core_announcement"."author_id",
                "core_announcement"."created_date",
                "core_announcement"."last_modified_date",
                "core_announcement"."show_after",
                "core_announcement"."title",
                "core_announcement"."body",
                "core_announcement"."organization_id",
                "core_announcement"."is_public",
                "core_announcement"."supervisor_id",
                "core_announcement"."status",
                "core_announcement"."rejection_reason"
FROM "core_announcement"
         INNER JOIN "core_organization" ON ("core_announcement"."organization_id" = "core_organization"."id")
         LEFT OUTER JOIN "core_user_organizations"
                         ON ("core_organization"."id" = "core_user_organizations"."organization_id")
         LEFT OUTER JOIN "core_organization_execs"
                         ON ("core_organization"."id" = "core_organization_execs"."organization_id")
WHERE ((("core_announcement"."status" = 'a' AND "core_announcement"."is_public") OR
        ("core_announcement"."status" = 'a' AND "core_user_organizations"."user_id" = 3) OR
        "core_organization_execs"."user_id" IN (3)) AND
       "core_announcement"."organization_id" = 1 AND
       "core_announcement"."id" IN (
           SELECT "announcement_id"
           FROM "core_announcement_tags"
           WHERE "tag_id" IN (10, 42)
           GROUP BY "announcement_id"
           HAVING COUNT(DISTINCT "tag_id") = 2
       ))
ORDER BY "core_announcement"."show_after" DESC

will fix asap but is not CRITICAL RN

JasonLovesDoggo commented 1 year ago
        return queryset.filter(Q(tags__in=[10]) and Q(tags__in=[47])) # works
        # vs
        return queryset.filter(Q(tags__in=[10]) & Q(tags__in=[47])) # does not

bitwise sucks.

todo: fix this 😭

JasonLovesDoggo commented 1 year ago

Non-applicable anymore. Search type was removed