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.
overlapin that case, check an example here: stackoverflow.com/questions/32747623/…column_propertyin your model and create a fake column from your arraytagsin your model to create the string from the array and then compare withcontains, 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