Closed SebCorbin closed 4 years ago
@SebCorbin there are tests that use Subquery
in conjunction with OuterRef
, so that is a hopeful sign.
Perhaps try attaching the extra where clause element directly on the subquery, outside of the CTE
- last_emissions_year=Subquery(qs[:1]),
+ last_emissions_year=Subquery(qs.filter(plant_id=OuterRef('pk'))[:1]),
Maybe that will change the meaning of your query in a bad way?
It's pretty hard for me to get a clear picture of what's going wrong without being able to run some code. Could you to write a failing test for django-cte or at least a query using the test models so I can reproduce the error?
Also, what version of Django are you using?
Yes, attaching the filter clause is modifying the query in a bad way. I need the reference inside the CTE, and after having a look a the tests, the OuterRef
is always used outside the With()
object.
I've tried adding a test with the PR #16 but I'm not comfortable this is a real query (and of course it could be done way simpler).
My version of Django is 3.0.5
.
I have a pretty complex query, but thanks to your package, I've managed to make it an ORM based one (which is useful for annotation).
But I've got a problem: I'm wondering why I can't use CTE in a subquery, here's the query I'm trying to achieve (yes, it's complex, and I've already simplified it):
Which translates as
The one above works fine but is missing
AND "observation".plant_id = V0.plant_id
in the CTE, and when I addplant_id=OuterRef('pk')
to the CTEI get the error mentioned, any ideas?