Comments (4)
Are you trying to do what was described in this comment (construct a query with a FROM
clause that does not include a model table)? I don't think that's currently possible.
from django-cte.
construct a query with a FROM clause that does not include a model table
Exactly :D
But the querying part do work, that's just the insertion part which is failing.
The SELECT is properly generated and it works without a model in the FORM:
>>> str(UserInfo.with_rank.all().query)
'WITH RECURSIVE cte AS (SELECT "issue39_userinfo"."id", "issue39_userinfo"."user_id", "issue39_userinfo"."points", DENSE_RANK() OVER (ORDER BY "issue39_userinfo"."points" DESC) AS "r" FROM "issue39_userinfo") SELECT "cte"."id", "cte"."user_id", "cte"."points", "cte"."r" AS "r" FROM "cte"'
I tried with the Django model as a FORM by slighly changing my return in def with_rank
to:
return (
with_rank.join(UserInfo, user_id=with_rank.col.user_id)
.with_cte(with_rank)
.annotate(r=with_rank.col.r)
)
I'm now having:
django.db.utils.OperationalError: Problem installing fixture '/tmp/cteissue/issue39/fixtures/initial.json': Could not load issue39.UserInfo(pk=1): near ")": syntax error
The query being:
WITH RECURSIVE cte AS (SELECT "issue39_userinfo"."id", "issue39_userinfo"."user_id", "issue39_userinfo"."points", DENSE_RANK() OVER (ORDER BY "issue39_userinfo"."points" DESC) AS "r" FROM "issue39_userinfo")
UPDATE "issue39_userinfo" SET "user_id" = ?, "points" = ?
WHERE "issue39_userinfo"."id" IN (
WITH RECURSIVE cte AS (SELECT "issue39_userinfo"."id", "issue39_userinfo"."user_id", "issue39_userinfo"."points", DENSE_RANK() OVER (ORDER BY "issue39_userinfo"."points" DESC) AS "r" FROM "issue39_userinfo")
)
Using a CTE as base_manager_name may not be a good idea :D I was just hoping to make this work:
In [1]: from issue39.models import *
In [2]: User.objects.first().hkis.r # Note `.r` is from the cte.
Out[2]: 1 # And yes it does work \o/
from django-cte.
construct a query with a FROM clause that does not include a model table
Actually, this is possible. It's covered in the newly written documentation: https://dimagi.github.io/django-cte/#selecting-from-a-common-table-expression
However, I'm not sure that is the real issue here. @JulienPalard Did you ever get this resolved?
from django-cte.
Hi! Currently an alteriative works, that I use in production: https://framagit.org/hackinscience/hkis-website/-/blob/2ad08645/hkis/models.py#L28
But the reproducer I liked in the first issue still raises the no such table: cte
.
from django-cte.
Related Issues (20)
- Improve documentation HOT 3
- is there a way to select from a cte without joining it with a model? HOT 5
- Usage in the context of time series HOT 1
- Combining with django-reversion HOT 2
- Can I count all children in Queryset annotate? HOT 7
- annotate() usage in recursion HOT 5
- Unneccessary outer join breaks query HOT 1
- With(empty_qs) throws Error HOT 1
- “no such column”/“column does not exist” errors in Django 4.2 HOT 9
- Not able to do full outer join with django_cte HOT 2
- hnej
- `.update()` does not work when accessing nested tables HOT 1
- Minor : typo in the doc
- How to use as default manager? HOT 1
- Prefetch objects with their nested children
- issue while trying to use with_cte on EmptyResultSet HOT 1
- issue while using cte with union querysets
- Django 4.2 error: each UNION query must have the same number of columns HOT 3
- it's wrong number when i wnat to get total number by using count()
- WITH query name specified more than once HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-cte.