Search code examples
pythonjsonormponyorm

Pony ORM order_by json integral value


Pony ORM appears to be sorting by the string of the Json value instead of the integral value. Is there a way to get this to order correctly?

Here's an example:

from pony.orm import (
    Database, Required, Optional, db_session, commit,
    show, Json)

db = Database()
db.bind('sqlite', filename=':memory:', create_db=True)

class Person(db.Entity):
    name = Required(str)
    body = Optional(Json, default={})

db.generate_mapping(create_tables=True)


with db_session:
    Person(name='bort', body={'age': 1})
    Person(name='baz', body={'age': 2})
    Person(name='bar', body={'age': 10})
    Person(name='foo', body={'age': 20})

    commit()

    people = Person.select().order_by(lambda p: p.body['age'])
    show(people)

Output:

id|name|body       
--+----+-----------
1 |bort|{'age': 1} 
3 |bar |{'age': 10}
2 |baz |{'age': 2} 
4 |foo |{'age': 20}

Is there a way to work around this, does Pony ORM not support this, or am I doing something wrong?


Solution

  • This issue was resolved in 0.7.3.