Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are strings passed to SQLAlchemy's .like() method

I'm building an interface on Flask using SQLAlchemy and part of it is a search API. Essentially a type-ahead input is calling the server with its value (for example an email) and then the server is performing a SQLalchemy query using .like in a filter like below

q = session.query(User).filter(User.email.like('%'+term+'%')).all()

This query isn't really returning anything useful and after the first few characters, nothing at all. But if I perform the same query with the term hardcoded, like so:

q = session.query(User).filter(User.email.like('%mysearchterm%')).all()

It will return results perfectly fine, so there's something with how I'm putting the term into the like() method but I really can't figure out what the issue is. The term is coming in from an ajax POST and the value is there on the server-side, just .like() isn't using it correctly.

By "nothing useful" I mean that the first sets of results coming back have nothing to do with the actual term being inputted, after a term of length higher than 3-4 no results are returned back despite matching items existing in the DB.

Any help greatly appreciated.

like image 570
jduren Avatar asked Sep 14 '12 05:09

jduren


1 Answers

Issues been resolved. This query sat inside of a larger query builder function which applied limit and offset to the query later in the function, because the limit and offset were higher than the amount of results back the returned result set was empty.

Can chock this one up to bad human error and lack of sleep.

like image 73
jduren Avatar answered Oct 13 '22 21:10

jduren