Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ requires-poetry = ">=2.0"
packages = [
{ include = "tortoise" }
]
version = "0.0.0"
version = "0.25.3"

[dependency-groups]
dev = [
Expand Down
14 changes: 8 additions & 6 deletions tortoise/contrib/postgres/functions.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
from pypika_tortoise.terms import Function, Term

DEFAULT_TEXT_SEARCH_CONFIG = "pg_catalog.simple"


class ToTsVector(Function):
"""
to to_tsvector function
"""

def __init__(self, field: Term) -> None:
super().__init__("TO_TSVECTOR", field)
def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG) -> None:
super().__init__("TO_TSVECTOR", config_name, field)


class ToTsQuery(Function):
"""
to_tsquery function
"""

def __init__(self, field: Term) -> None:
super().__init__("TO_TSQUERY", field)
def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG) -> None:
super().__init__("TO_TSQUERY", config_name, field)


class PlainToTsQuery(Function):
"""
plainto_tsquery function
"""

def __init__(self, field: Term) -> None:
super().__init__("PLAINTO_TSQUERY", field)
def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG) -> None:
super().__init__("PLAINTO_TSQUERY", config_name, field)


class Random(Function):
Expand Down
10 changes: 4 additions & 6 deletions tortoise/contrib/postgres/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ class Comp(Comparator):
search = " @@ "


class SearchCriterion(BasicCriterion):
class SearchCriterion(BasicCriterion): # type: ignore
def __init__(self, field: Term, expr: Term | Function) -> None:
if isinstance(expr, Function):
_expr = expr
else:
_expr = ToTsQuery(expr)
super().__init__(Comp.search, ToTsVector(field), _expr)
if not isinstance(expr, Function):
expr = ToTsQuery(expr)
super().__init__(Comp.search, ToTsVector(config_name=expr.args[0].value, field=field), expr)
Copy link

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accessing expr.args[0].value assumes the first argument is always a literal config name; this is brittle. Consider adding a dedicated config_name attribute or accessor on your Function subclasses to make this extraction safer.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quizá tendría sentido validarlo antes de intentar desempaquetarlo

quizá también tendría sentido mejorar la readability manteniendo el orden original y la opcionalidad de la config

Loading