Skip to content

Instantly share code, notes, and snippets.

@diox
Created April 11, 2011 11:00
Show Gist options
  • Select an option

  • Save diox/913360 to your computer and use it in GitHub Desktop.

Select an option

Save diox/913360 to your computer and use it in GitHub Desktop.

Revisions

  1. diox created this gist Apr 11, 2011.
    23 changes: 23 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    from django.db import models

    class Foo(models.Model):
    bar = models.PositiveSmallIntegerField(default=1)
    class Meta:
    ordering = ['bar']

    class Child(Foo):
    barchild = models.PositiveSmallIntegerField(default=1)


    >>> str(Child.objects.order_by('pk').query)
    'SELECT "test_pk_foo"."id", "test_pk_foo"."bar", "test_pk_child"."foo_ptr_id", "test_pk_child"."barchild" FROM "test_pk_child" INNER JOIN "test_pk_foo" ON ("test_pk_child"."foo_ptr_id" = "test_pk_foo"."id") ORDER BY "test_pk_foo"."bar" ASC'

    # order_by('pk') doesn't do anything, the default ordering is used instead! (Tested with
    # sqlite3 and postgresql_psycopg2 engines)

    >>> str(Child.objects.order_by('id').query)
    'SELECT "test_pk_foo"."id", "test_pk_foo"."bar", "test_pk_child"."foo_ptr_id", "test_pk_child"."barchild" FROM "test_pk_child" INNER JOIN "test_pk_foo" ON ("test_pk_child"."foo_ptr_id" = "test_pk_foo"."id") ORDER BY "test_pk_child"."foo_ptr_id" ASC'

    # order_by('id') works.

    # Note that removing the default ordering in the parent Model also fixes the bug.