0

I need to filter a query result by a ARRAY of STRINGS Column, on Postgres database. And need it to be case insensitive, working as 'ilike' postgres function. Here is the query that I need to work in FLask-SQLAlchemy syntax:

select * from articles a 
where array_to_string(a.tags, ',') ilike any (array['%football%', '%basketball%'])

I've already tried these syntaxes:

Article.query.filter(Article.tags.contains(f"{{{tags}}}")).all()
Article.query.filter(Article.tags.ilike(f"%{tags}%")).all()
Article.query.filter(Article.tags.in_(tags).all()

Where tags is a list of strings like this:

tags = [
            'football', 
            'basketball', 
            'hockey', 
            'soccer', 
            'baseball', 
            'golf', 
            'fighting', 
            'tennis'
        ]

SQLAlchemy model definition:

class Article(db.Model):
    __tablename__ = 'articles'

    article_id = db.Column(db.BigInteger, primary_key=True, autoincrement=False)
    headline = db.Column(db.String)
    source = db.Column(db.String)
    summary = db.Column(db.String)
    tags = db.Column(ARRAY(db.String), default=[])
    timestamp = db.Column(db.DateTime)
    url = db.Column(db.String)
    .
    .
    .

I've already checked the documentation and another similar questions, and didn't find any solution that fits my needs.

3
  • I think you can use overlap in that case, check an example here: stackoverflow.com/questions/32747623/… Commented Nov 15, 2021 at 21:24
  • 1
    Works if I have a single word in each item of that tag column, sometimes I have like "Euro Football" and I just want to filter by "football", case insensitive, and need it to find the word inside a bigger string as: ilike ('%football%') Commented Nov 15, 2021 at 21:51
  • now I understand in that case maybe you can use column_property in your model and create a fake column from your array tags in your model to create the string from the array and then compare with contains, I will try to create the scenario but I need to prepare my environment. Check here to see what I'm talking about docs.sqlalchemy.org/en/14/orm/mapped_sql_expr.html Commented Nov 15, 2021 at 21:57

1 Answer 1

1

To make like the query that are you talking about you can use the functions array_to_string and _any from SQLAlchemy functions for example like this:

from sqlalchemy import func
articles = Article.query.filter(
    func.array_to_string(Article.tags, ','). \
        ilike(func.any_(['%fighting%', '%golf%']))
    ).all()

Inside the any_ function you can pass down your array to make the search.

See any_ documentation sqlalchemy any_ column modifier.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.