Fixup: make common fields use fast queries

I surprisingly found that many queries for fields that exist in the
database are 'slow'. Querying items with 'month:0' used a slow query
while 'title:0' used a fast one.

I found the mistake in the logic: the field existence in the db check
was done *after* prepending the table name to the field. This meant that
the logic wrongly checked e.g. 'items.field' instead of 'field'.

This commit fixes the above and renames the variables for more clarity.
This commit is contained in:
Šarūnas Nejus 2024-08-11 21:35:28 +01:00
parent 7703c9e338
commit c216b636ac
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435

View file

@ -152,14 +152,15 @@ def construct_query_part(
# Field queries get constructed according to the name of the field
# they are querying.
else:
key = key.lower()
if key in model_cls.shared_db_fields:
field = table = key.lower()
if field in model_cls.shared_db_fields:
# This field exists in both tables, so SQLite will encounter
# an OperationalError if we try to query it in a join.
# Using an explicit table name resolves this.
key = f"{model_cls._table}.{key}"
table = f"{model_cls._table}.{field}"
out_query = query_class(key, pattern, key in model_cls.all_db_fields)
field_in_db = field in model_cls.all_db_fields
out_query = query_class(table, pattern, field_in_db)
# Apply negation.
if negate: