Skip to content

order_by orders Json numbers like strings  #284

@jamesstidard

Description

@jamesstidard

It appears Pony is stringifying the values in json before it it orders so numbers are sorted alphabetically. Is this a known limitation?

Here's an example script:

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)
id|name|body       
--+----+-----------
1 |bort|{'age': 1} 
3 |bar |{'age': 10}
2 |baz |{'age': 2} 
4 |foo |{'age': 20}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions