“Too many SQL variables” error in django with sqlite3

后端 未结 6 1940
臣服心动
臣服心动 2020-12-10 00:23

I\'m getting this error using sqlite3 in django:

Exception Value: too many SQL variables

And I think the answer to it is this, from

6条回答
  •  死守一世寂寞
    2020-12-10 01:07

    Note: I am not Django expert

    The problem is that in your query

    vals = Company.objects.filter(id__in=comp_ids)... 
    

    you pass a bloated set of values and the underlaying engine cannot execute the query. Fortunately sqlalchemy, that you execute in that line, is lazy. You an execute a SELECT and then filter the entries "manually" without big performance penalties.

    It would look more-less like that:

    # here you should do a proper session.query(CompanyTable)
    vals_all = Company.objects
    
    vals_filtered = (item for item in vals_all if item.id in comp_ids)
    vals_ordered = sorted(vals_filtered, key=attrgetter('name'))
    vals_final = [(v.id, v.name) for v in vals_ordered]
    

提交回复
热议问题