简体   繁体   English

Django ORM 创建嵌套查询防止使用“有”

[英]Django ORM create nested query prevent using `having`

Assume I have a Transaction model which has following fields [token, pair_token, amount, related_transaction] , I want to generate a query like this in mysql:假设我有一个Transaction model 具有以下字段[token, pair_token, amount, related_transaction] ,我想在 mysql 中生成这样的查询:

SELECT token_id, pair_token_id, (
  SELECT ABS(t3.amount / t2.amount) price
    FROM 
    Transaction t2 join Transaction t3 on t2.related_transaction_id=t3.id 
    WHERE t2.id=MAX(t1.id)) 
FROM 
Transaction t1 
WHERE 
t1.token_id in (1, 2, 3, 5, 6) and t2.pair_token_id in (4) and t1.timestamp > CURRENT_TIMESTAMP - interval 24 hour 
GROUP BY
token_id, pair_token_id;

This query finds the last value of two related transaction which equals the price of a combination of a token and its pair_token.此查询查找两个相关交易的最后一个值,该值等于令牌及其 pair_token 组合的价格。 To get a similar results inside django I have used Subquery as below:为了在 django 中得到类似的结果,我使用了如下Subquery

trs = Transaction.objects.filter(token_id__in=[1, 2, 3], pair_token_id__in=[4], timestamp__gt=Now()-timedelta(hours=24))
last_price = Transaction.objects.annotate(price=Abs(F('related_transaction__amount') / F('amount')))
trs = trs.values('token_id', 'pair_token_id').annotate(
    price=Subquery(last_price.filter(id=Max(OuterRef('id'))).values('price'))).\
values('token_id', 'pair_token_id', 'price')

But it generates a wrong query:但它会生成错误的查询:

SELECT
`Transaction`.`token_id`, `Transaction`.`pair_token_id`, (
        SELECT ABS((U1.`amount` / U0.`amount`)) AS `price` 
        FROM 
        `Transaction` U0 LEFT OUTER JOIN `Transaction` U1 ON (U0.`related_transaction_id` =U1.`id`) 
        HAVING U0.`id` = MAX(`Transaction`.`id`)) AS `price` 
FROM 
`Transaction` 
WHERE (`Transaction`.`pair_token_id` IN (4) AND `Transaction`.`timestamp` > (CURRENT_TIMESTAMP - INTERVAL 86400000000 MICROSECOND) AND `Transaction`.`token_id` IN (1, 2, 3, 5, 6))

Mysql generates an error for this query and it must be, I do not know how to avoid generating a having query in using Subquery . Mysql 会为此查询生成一个错误,它一定是,我不知道如何避免在使用 Subquery having生成一个Subquery If I use this query without any Subquery a group by clause would be generated but in using Subquery group by term would be removed and having appears.如果我在没有任何子Subquery的情况下使用此查询,则会生成一个 group by 子句,但在使用Subquery group by term 时将被删除并having

I am using django 3.1.1 and mysql 8.0.19我正在使用django 3.1.1mysql 8.0.19

Update :更新

Transaction model:事务 model:

class Token:
    name = models.CharField(max_length=20)

class Transaction:
    token = models.ForeignKey(
        Token, blank=False, null=False, on_delete=models.CASCADE
    )
    pair_token = models.ForeignKey(
        Token, blank=True, null=True, on_delete=models.SET_NULL
    )

    related_transaction = models.ForeignKey(
        Transaction, blank=True, null=True, on_delete=models.PROTECT
    )

    amount = models.DecimalFeild(
        max_digits=10, price_decimals=3
    )


t1 = Token.objects.create(name='T1')
t2 = Token.objects.create(name='T2')
t3 = Token.objects.create(name='T3')

tr11 = Transaction.objects.create(
    token=t1, pair_token=t2, amount=Decimal('2.4')
)

tr12 = Transaction.obejcts.create(
    token=t2, pair_token=t1, related_transaction=tr11, amount=Decimal('3')
)

tr21 = Transaction.objects.create(
    token=t1, pair_token=t2, amount=Decimal('1.4')
)

tr22 = Transaction.obejcts.create(
    token=t2, pair_token=t1, related_transaction=tr21, amount=Decimal('3')
)


# If I want to get price of t2 in t1 I must divide tr21.amount / tr22.amount
# Notice that there are many transactions related to each pair and I want to just list of the last price of each pairs.  

The following should give you the result you want.下面应该给你你想要的结果。

First we create a subquery that calculates the price for a transaction, filters by the outer token/pair_token and is then ordered so that the max id is first首先,我们创建一个计算交易价格的子查询,通过外部令牌/pair_token 进行过滤,然后进行排序,以便最大 id 排在第一位

latest_prices = Transaction.objects.filter(
    related_transaction__isnull=False
).annotate(
    price=Abs(F('related_transaction__amount') / F('amount'))
).filter(
    token=OuterRef('token'),
    pair_token=OuterRef('pair_token')
).order_by('-id')

Then use this subquery to annotate each row with the latest price for the token/pair_token and use distinct to get unique values然后使用这个子查询用 token/pair_token 的最新价格注释每一行,并使用distinct获取唯一值

Transaction.objects.filter(
    related_transaction__isnull=False
).annotate(
    price=Subquery(latest_prices.values('price')[:1])
).values('token_id', 'pair_token_id', 'price').distinct()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM