question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No way to combine case-insensitive contains() with autoescape

See original GitHub issue

Migrated issue, originally created by v (@vr2262)

I would like to filter a query with ILIKE while escaping special characters like % and _. I tried this:

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()


class Example(Base):
    __tablename__ = 'example'
    id = Column(Integer, primary_key=True)
    string_col = Column(String)


session = sessionmaker()()
session.add(Example(string_col='S%mething'))

search_string = 's%'
print(
    session.query(Example)
    .filter(
        func.lower(Example.string_col)
        .contains(func.lower(search_string), autoescape=True)
    )
)

But I get the error TypeError: String value expected when autoescape=True because contains() expects an actual string as the first argument.

Ideally SQLAlchemy would provide this feature out of the box, perhaps with icontains() or contains(insensitive=True, ...).

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
sqlalchemy-botcommented, Nov 27, 2018

Michael Bayer (@zzzeek) wrote:

all you have to do is call lower() on the string:

print(
    session.query(Example)
    .filter(
        func.lower(Example.string_col)
        .contains(search_string.lower(), autoescape=True)
    )
)

that’s the beauty of it being a string in the first place.

icontains() is already #3482.

0reactions
sqlalchemy-botcommented, Nov 27, 2018

Changes by v (@vr2262):

  • removed labels: duplicate
Read more comments on GitHub >

github_iconTop Results From Across the Web

Case insensitive 'Contains(string)' - Stack Overflow
Great string extension method! I've edited mine to check the source string is not null to prevent any object reference errors from occurring...
Read more >
Match regular expression (case sensitive) - MATLAB regexp
This MATLAB function returns the starting index of each substring of str that matches the character patterns specified by the regular expression.
Read more >
String | Functions | AQL | ArangoDB Documentation
The string matching performed by CONTAINS() is case-sensitive. To determine if or at which position a value is included in an array, see...
Read more >
WHERE - Cypher Manual - Neo4j
The CONTAINS operator is used to perform case-sensitive matching regardless of location within a string. Query. Cypher. Query. Copy to Clipboard Run in...
Read more >
Impala String Functions
If the argument string to BASE64DECODE() does not represent a valid ... that change how the match is performed, such as i for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found