tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.7k stars 391 forks source link

Support annotations and joins in F() #1761

Closed henadzit closed 2 weeks ago

henadzit commented 3 weeks ago

Description

This PR:

In order to achieve that, the following implementation details changed

As part of this PR, I had to change how custom functions can be defined. It changed from

from tortoise.expressions import F
from pypika.terms import Function

class JsonSet(Function):
    def __init__(self, field: F, expression: str, value: Any):
        super().__init__("JSON_SET", field, expression, value)

to

from tortoise.functions import Function
from pypika.terms import Function as PupikaFunction

# it is a tortoise Function now, not Pypika's Function
class PypikaJsonSet(Function):
    def __init__(self, field: F, expression: str, value: Any):
        super().__init__("JSON_SET", field, expression, value)

    database_func = PypikaJsonSet

Effectively now custom functions are supposed to be a tortoise Function, not a Pypika's Function. The change had to be done because the Pypika's Function is no longer compatible with F and, frankly, having the interface that accepts both Pypika's functions and tortoise functions is a bit messy and makes the code quite convoluted.

Motivation and Context

This introduces functionality supported by Django but also there is a clear demand for these features in the tortoise community. The following issues should be fixed by this PR

How Has This Been Tested?

Checklist:

coveralls commented 3 weeks ago

Pull Request Test Coverage Report for Build 11742066623

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
tortoise/backends/base/executor.py 13 14 92.86%
tortoise/expressions.py 139 141 98.58%
<!-- Total: 216 219 98.63% -->
Files with Coverage Reduction New Missed Lines %
tortoise/queryset.py 1 94.47%
tortoise/backends/base/executor.py 1 91.35%
<!-- Total: 2 -->
Totals Coverage Status
Change from base Build 11660093686: 0.09%
Covered Lines: 6084
Relevant Lines: 6709

💛 - Coveralls