Skip to content

CTE statement is not being generated #8

@nirizr

Description

@nirizr

First of all, sorry for posting this as an issue. I could not think of any other place to ask. This is the first time I'm trying django-cte so my mistake could be silly, however I mostly copied the README example.

This is my CTE code:

class Annotation(models.Model):
 objects = django_cte.CTEManager()

 uuid = models.UUIDField(null=True, unique=True)
 instance = models.ForeignKey(Instance, models.CASCADE,
                              related_name='annotations')
 type = models.CharField(max_length=64, choices=TYPE_CHOICES)
 data = models.TextField()
 dependencies = models.ManyToManyField('self', symmetrical=False,
                                       related_name="dependents",
                                       through="Dependency")

  def make_regions_cte(cte):
       return Annotation.objects.filter(
           # start with root nodes
           id__in=annotation_ids
       ).values(
           "id",
           "uuid",
           depth=value0,
       ).union(
           # recursive union: get descendants
           cte.join(Annotation, uuid=cte.col.uuid).values(
               "id",
               "uuid",
               depth=cte.col.depth + value1,
           ),
           all=True,
       )

   cte = With.recursive(make_regions_cte)

   annotations = (
       cte.join(Annotation, uuid=cte.col.uuid)
       .with_cte(cte)
       .annotate(
           depth=cte.col.depth,
       )
       .order_by("depth")
   )

This is the resulting SQL query:

WITH RECURSIVE cte AS ((SELECT "collab_annotation"."id", "collab_annotation"."uuid", ("cte"."depth" + 1) AS "depth" FROM "collab_annotation" INNER JOIN "cte" ON "collab_annotation"."uuid" = ("cte"."uuid"))) SELECT "collab_annotation"."id", "collab_annotation"."uuid", "collab_annotation"."instance_id", "collab_annotation"."type", "collab_annotation"."data", "cte"."depth" AS "depth" FROM "collab_annotation" INNER JOIN "cte" ON "collab_annotation"."uuid" = ("cte"."uuid") ORDER BY "depth" ASC  LIMIT 21; args=(1,)

This is the error I'm getting:

django.db.utils.ProgrammingError: recursive query "cte" does not have the form non-recursive-term UNION [ALL] recursive-term
LINE 1: WITH RECURSIVE cte AS ((SELECT "collab_annotation"."id", "co...

Thanks for reading through!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions