Coalesce must take at least two expressions

五迷三道 提交于 2019-12-11 17:14:25

问题


I'm using Django and Python 3.7. I want to write a Coalesce expression to help me write a larger Django query. I have

            Coalesce(
                F("votes")
                -
                Subquery(relevant_hour_stats.values('votes_threshold')[:1]),
                output_field=models.FloatField())

Here is the expression in context ...

qset = (
    ArticleStat.objects
        .all()
        .annotate(
        shifted_article_create_hour=ExtractHour(ExpressionWrapper(
            F('article__created_on')
            +
            timedelta(seconds=avg_fp_time_in_seconds),
            output_field=models.DateTimeField()
        ))
    )
        .annotate(
        votes_above_threshold=(
                Coalesce(
                    F("votes")
                    -
                    Subquery(relevant_hour_stats.values('votes_threshold')[:1]),
                    output_field=models.FloatField())
        ),
    )
        .filter(
        votes_above_threshold__gt=0,
    )
)

but this is resulting in a

Coalesce must take at least two expressions

complaining about the line

output_field=models.FloatField()

as far as I can tell, I have two expressions. What else could the error be referring to?


回答1:


"Expression" in the terms of django are instances of django.db.models.expressions.Expression.

F('votes') — expression.

Subquery(...) — expression too.

But expression+expression == combined_expression so F(...) - Subquery(...) is a single "complex" expression.

What you need is a second POSITIONAL argument for Coalesce:

Coalesce(
    (F(...) - Subquery(relevant_hour_stats.values('votes_threshold')[:1])),  # First expression
    Value(0.0),  # Second expression
    output_field=models.FloatField()
)

And I think that subquery can potentially result in NULL (not F()), so it's better to wrap only Subquery in the Coalesce:

qset = (
    ArticleStat.objects
        .all()
        .annotate(
            shifted_article_create_hour=ExtractHour(
                ExpressionWrapper(
                    F('article__created_on') + timedelta(seconds=avg_fp_time_in_seconds),
                    output_field=models.DateTimeField()
                )
            ),
        )
        .annotate(
            votes_above_threshold=(  
                # Single expression F()-Coalesce(...) wrapped in parenthesis
                F("votes") - Coalesce(
                    # 2 expressions as arguments to Coalesce:
                    Subquery(relevant_hour_stats.values('votes_threshold')[:1]),
                    Value(0.0),
                    output_field=models.FloatField(),
                )
            ),
        )
        .filter(
            votes_above_threshold__gt=0,
        )
)


来源:https://stackoverflow.com/questions/55978773/coalesce-must-take-at-least-two-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!