gma*_*map 5 python django postgresql orm
我正在尝试编写一个查询,该查询检索应通知其所有者到期的所有订阅。
我想排除已通知的订阅和具有较新订阅可用的订阅。
接下来是查询:
Subscription.objects.filter(
end_date__gte=timezone.now(),
end_date__lte=timezone.now() + timedelta(days=14),
).exclude(
Q(notifications__type=Notification.AUTORENEWAL_IN_14) | Q(device__subscriptions__start_date__gt=F('start_date'))
)
Run Code Online (Sandbox Code Playgroud)
如果没有该| Q(device__subscriptions__start_date__gt=F('start_date')部分,查询将完美运行。有了它,django (postgres) 会引发下一个错误:
django.db.utils.ProgrammingError: missing FROM-clause entry for table "u1"
LINE 1: ...ption" U0 INNER JOIN "orders_subscription" U2 ON (U1."id" = ...
Run Code Online (Sandbox Code Playgroud)
我检查了sql,似乎不正确:
SELECT "orders_subscription"."id",
"orders_subscription"."months",
"orders_subscription"."end_date",
"orders_subscription"."start_date",
"orders_subscription"."order_id",
"orders_subscription"."device_id",
FROM "orders_subscription"
WHERE ("orders_subscription"."churned" = false
AND "orders_subscription"."end_date" >= '2019-04-05T13:27:39.808393+00:00'::timestamptz
AND "orders_subscription"."end_date" <= '2019-04-19T13:27:39.808412+00:00'::timestamptz
AND NOT (("orders_subscription"."id" IN
(SELECT U1."subscription_id"
FROM "notifications_notification" U1
WHERE (U1."type" = 'AUTORENEWAL_IN_2W'
AND U1."subscription_id" IS NOT NULL))
OR ("orders_subscription"."device_id" IN
(SELECT U2."device_id"
FROM "orders_subscription" U0
INNER JOIN "orders_subscription" U2 ON (U1."id" = U2."device_id")
WHERE (U2."start_date" > (U0."start_date")
AND U2."device_id" IS NOT NULL))
AND "orders_subscription"."device_id" IS NOT NULL)))) LIMIT 21
Execution time: 0.030680s [Database: default]
Run Code Online (Sandbox Code Playgroud)
这是导致问题的部分:
INNER JOIN "orders_subscription" U2 ON (U1."id" = U2."device_id")
WHERE (U2."start_date" > (U0."start_date")
AND U2."device_id" IS NOT NULL))
Run Code Online (Sandbox Code Playgroud)
U1 没有在任何地方定义(它在另一个子句中本地定义,但这并不重要。
关系模型非常简单,一个设备可以有很多订阅,一个订阅可以有很多(不同的)通知。
class Subscription(models.Model):
end_date = models.DateTimeField(null=True, blank=True)
start_date = models.DateTimeField(null=True, blank=True)
device = models.ForeignKey(Device, on_delete=models.SET_NULL, null=True, blank=True, related_name="subscriptions")
# Other non significatn fields
class Device(models.Model):
# No relational fields
class Notification(models.Model):
subscription = models.ForeignKey('orders.Subscription', related_name="notifications", null=True, blank=True, on_delete=models.SET_NULL)
# Other non significatn fields
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:我的查询是错误的还是 Django ORM 查询生成器中的错误?
ORM 显然无法将您的子句转换为 SQL。即使单独使用该子句(没有前面的子句,即U1查询中的任何位置都没有别名),也会发生这种情况。
除了不存在的别名之外,ORM 似乎还错误地识别了F(\'start_date\')\xe2\x80\x93 的来源,它是主表orders_subscription(没有别名的表),而不是子查询中的任何别名表。
您可以通过自己定义适当的子查询来帮助 ORM。
\n\n(下面的尝试基于这样的假设:该子句的目的是排除具有较晚日期的同级订阅(= 相同父设备)的订阅。)
\n\n这是带有更正子句的排除过滤器:
\n\nfrom django.db.models import OuterRef, Subquery\n\nqs.exclude(\n Q(notifications__type=Notification.AUTORENEWAL_IN_14) |\n Q(device_id__in=Subquery(Subscription.objects\n .filter(\n device_id=OuterRef(\'device_id\'), \n start_date__gt=OuterRef(\'start_date\'))\n .values(\'device_id\')\n ))\n)\nRun Code Online (Sandbox Code Playgroud)\n\n然而,更仔细地观察过滤器,我们选择了一个列 ( device_id),我们刚刚将其值作为过滤条件传递。这可以通过Exists子查询更好地表达:
from django.db.models import OuterRef, Exists\n\n(qs.annotate(has_younger_siblings=Exists(Subscription.objects.filter(\n device_id=OuterRef(\'device_id\'), \n start_date__gt=OuterRef(\'start_date\'))))\n .exclude(has_younger_siblings=True)\n .exclude(notifications__type=Notification.AUTORENEWAL_IN_14)\n)\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2278 次 |
| 最近记录: |