0

Im reformatting some old code, and I have

        query = db.engine.execute('SELECT *, ( SELECT min(levenshtein(a, \'{}\''.format(word) + ', 1, 1, 1)) FROM ' +
                              'regexp_split_to_table(description_unaccent, \' \') as a) as distance FROM '
                              '"BD_TABLE" ORDER BY distance')

and I tried to modify to:

    dist = func.levenshtein(BD_TABLE.description_unaccent, data['text'])
    query = BD_TABLE.query.filter(dist <= 5).order_by(dist).limit(10)

but I'm failing to get the distance column

(SELECT *, ( SELECT min(levenshtein(a,word, 1, 1, 1)) FROM
regexp_split_to_table(description_unaccent,word) as a) as distance)

I read about alias and label, but I couldn't manage to use it after as a variable, because later I wanted to do something as:

    for row as query:
        print(row.distance)
set92
  • 322
  • 4
  • 13
  • It's good that you're modifying the original that uses string formatting for passing values, which is an abomination. For future reference read on using textual SQL with SQLAlchemy, especially the parts about using placeholders/bindparams: http://docs.sqlalchemy.org/en/latest/core/tutorial.html#using-textual-sql – Ilja Everilä Nov 27 '17 at 07:08
  • Related https://stackoverflow.com/questions/11530196/flask-sqlalchemy-query-specify-column-names. – Ilja Everilä Nov 27 '17 at 07:13

0 Answers0