【发布时间】:2020-06-16 08:18:08
【问题描述】:
我正在使用 postgresql。
我在子查询中编写子查询我想在父表的id 上应用过滤器和子表的字段。但它显示错误
查询:
orders = Order.objects.filter(user=request.user, status='P')
albums = models.Album.objects.annotate(purchased_status=Subquery(orders.values('id').filter(item_id=OuterRef('id'))[:1]))
错误
django.db.utils.ProgrammingError: operator does not exist: character varying = integer
LINE 1: ...atus" = 'P' AND U0."user_id" = 1 AND U0."item_id" = "bookcen...
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
如果我在除 id 之外的任何字段上应用过滤器,它就可以正常工作。如果我将父表的id 添加为子表中的foreign key 但不想这样做,则此错误修复。
如果因为 OuterRef('id') 返回引用 id 而不是像 1 或 2 这样的整数值,则会出现问题
OuterRef('id') 有什么方法可以返回 id 的整数值。
【问题讨论】:
标签: python django postgresql