Is your feature request related to a problem? Please describe.
As of v0.13.2 there is no support for database functions. Depending on your use-case, query fields might require extra processing:
Describe the solution you'd like
Add support for functions via an importable module (e.g. tortoise.models.functions)
that implements database functions (like COALESCE) and can wrap model fields possibly via annotations like:
from tortoise.models.functions import Coalesce
class User(Model):
id = fields.IntField(pk=True)
name = fields.TextField()
email = fields.TextField()
username = fields.TextField()
join_date = fields.DateTimeField()
users = await User.annotate(
screen=Coalesce("name", "username", "email"),
clean_join_date=Coalesce('join_date', datetime.datetime(2011, 12, 31),
).filter(
clean_join_date__gt=datetime.datetime(2012, 1, 1)
).values_list('screen', 'clean_join_date')
Describe alternatives you've considered
The two most well known ORMs for python SQLAlchemy and Django ORM have similar APIs:
Additional context
The underpinning SQL library pypika does implement and have documented the most important database functions.
From the pypika documentation for example of string functions:
from pypika import functions as fn
customers = Tables('customers')
q = Query.from_(customers).select(
customers.id,
fn.Concat(customers.fname, ' ', customers.lname).as_('full_name'),
)
Thank you for this well presented/thought out feature request @zoliszeredi
We already have support for some aggregate functions, and I don't see much new required to support other functions.
(Although the name "Aggregate" is too specific, we should probably change it to "Function")
Looking at the code, adding single-parameter functions will be trivial, multi-parameter functions will need a refactor. Shouldn't be too difficult.
Do you want to have a go?
Out of the myriad of functions the one that I really need is COALESCE to do easier comparison for numerical, date types when these are incomplete, missing(e.g. NULL).
I'd like to how far I can go for simpler functions by changing as you suggested Aggregate to the more generic Function.
Most helpful comment
Out of the myriad of functions the one that I really need is
COALESCEto do easier comparison for numerical, date types when these are incomplete, missing(e.g. NULL).I'd like to how far I can go for simpler functions by changing as you suggested Aggregate to the more generic Function.