work around new sqlalchemy limitations

This commit is contained in:
j 2024-06-08 12:45:21 +01:00
parent c14d250166
commit 60e17ab076
2 changed files with 14 additions and 8 deletions

View file

@ -5,7 +5,7 @@ import os
import unicodedata import unicodedata
from sqlalchemy.orm import load_only from sqlalchemy.orm import load_only
from sqlalchemy.sql.expression import text from sqlalchemy.sql.expression import text, column
from sqlalchemy import func from sqlalchemy import func
from oxtornado import actions from oxtornado import actions
@ -58,8 +58,13 @@ def find(data):
qs = models.Find.query.filter_by(key=q['group']) qs = models.Find.query.filter_by(key=q['group'])
if items is None or items.first(): if items is None or items.first():
if items is not None: if items is not None:
qs = qs.filter(models.Find.item_id.in_(items)) ids = [i[0] for i in items.with_entities(column('id'))]
values = list(qs.values('value', 'findvalue', 'sortvalue')) qs = qs.filter(models.Find.item_id.in_(ids))
values = list(qs.values(
column('value'),
column('findvalue'),
column('sortvalue'),
))
for f in values: for f in values:
value = f[0] value = f[0]
findvalue = f[1] findvalue = f[1]

View file

@ -5,7 +5,7 @@ import unicodedata
import sqlalchemy.orm.exc import sqlalchemy.orm.exc
from sqlalchemy.sql import operators from sqlalchemy.sql import operators
from sqlalchemy.orm import load_only from sqlalchemy.orm import load_only
from sqlalchemy.sql.expression import text from sqlalchemy.sql.expression import text, column
import utils import utils
import settings import settings
@ -14,6 +14,7 @@ from fulltext import find_fulltext
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def get_operator(op, type='str'): def get_operator(op, type='str'):
return { return {
'str': { 'str': {
@ -135,7 +136,8 @@ class Parser(object):
q = get_operator(op)(self._find.findvalue, v) q = get_operator(op)(self._find.findvalue, v)
if k != '*': if k != '*':
q &= (self._find.key == k) q &= (self._find.key == k)
ids = self._model.query.join(self._find).filter(q).options(load_only('id')) ids = self._find.query.filter(q).with_entities(column('item_id'))
ids = [i[0] for i in ids]
return self.in_ids(ids, exclude) return self.in_ids(ids, exclude)
elif k == 'list': elif k == 'list':
nickname, name = v.split(':', 1) nickname, name = v.split(':', 1)
@ -269,5 +271,4 @@ class Parser(object):
data.get('query', {}).get('operator', '&')) data.get('query', {}).get('operator', '&'))
for c in conditions: for c in conditions:
qs = qs.filter(c) qs = qs.filter(c)
#print(qs)
return qs return qs