Opened 6 years ago

Closed 6 years ago

#29743 closed Cleanup/optimization (invalid)

Queryset.extra is required to select Postgres' system columns like xmin and xmax

Reported by: Navtez Owned by: nobody
Component: Database layer (models, ORM) Version: 2.1
Severity: Normal Keywords: orm, queryset, models
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

We use Queryset.extra to select Postgres' system columns like xmin and xmax as follows:

Model.objects.extra(select={'version': 'xmin'})

Please don't remove this as we don't find an alternative to use system columns.

Thanks!

Change History (1)

comment:1 by Simon Charette, 6 years ago

Resolution: invalid
Status: newclosed

You should be able to define Expression subclasses for this purpose.

from django.db.models import Expression

class XMin(Expression):
    output_field = models.PositiveIntegerField()

    def as_postgresql(self, compiler, connection):
        return 'xmin', ()

class XMax(Expression):
    output_field = models.PositiveIntegerField()

    def as_postgresql(self, compiler, connection):
        return 'xmax', ()

Model.objects.annotate(version=XMin())

It should be possible to adapt the expressions to convert related references to table aliases as well (e.g. XMin('user') -> user.xmin) as well.

Note: See TracTickets for help on using tickets.
Back to Top