小马ORM order_by json 整数值
Pony ORM order_by json integral value
Pony ORM 似乎按 Json 值的字符串而不是整数值排序。有没有办法让它正确排序?
这是一个例子:
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}
有没有办法解决这个问题,Pony ORM 不支持这个,还是我做错了什么?
Comment: causes Pony to raise and exception:
"TypeError: Function 'int' cannot be used this way: int(p.body['age'])"
无法用 pony
验证,请尝试 pony
是否接受:
def _int(v):
return int(v)
people = Person.select().order_by(lambda p: _int(p.body['age']))
Question: Is there a way to work around this, does Pony ORM not support this
您正在使用 sqlite
,关于文档 json
是 str
。
解决方法:
lambda p: int(p.body['age'])
给我 NO 纯 Python 错误。
Pony ORM: JSON Support in Databases
For storing JSON in the database Pony uses the following types:
SQLite - TEXT
PostgreSQL - JSONB (binary JSON)
MySQL - JSON (binary JSON, although it doesn’t have ‘B’ in the name)
Oracle - CLOB
Starting with the version 3.9 SQLite provides the JSON1 extension module. This extension improves performance when working with JSON queries, although Pony can work with JSON in SQLite even without this module.
此问题已在 0.7.3
中解决。
Pony ORM 似乎按 Json 值的字符串而不是整数值排序。有没有办法让它正确排序?
这是一个例子:
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}
有没有办法解决这个问题,Pony ORM 不支持这个,还是我做错了什么?
Comment: causes Pony to raise and exception:
"TypeError: Function 'int' cannot be used this way: int(p.body['age'])"
无法用 pony
验证,请尝试 pony
是否接受:
def _int(v):
return int(v)
people = Person.select().order_by(lambda p: _int(p.body['age']))
Question: Is there a way to work around this, does Pony ORM not support this
您正在使用 sqlite
,关于文档 json
是 str
。
解决方法:
lambda p: int(p.body['age'])
给我 NO 纯 Python 错误。
Pony ORM: JSON Support in Databases
For storing JSON in the database Pony uses the following types:SQLite - TEXT PostgreSQL - JSONB (binary JSON) MySQL - JSON (binary JSON, although it doesn’t have ‘B’ in the name) Oracle - CLOB
Starting with the version 3.9 SQLite provides the JSON1 extension module. This extension improves performance when working with JSON queries, although Pony can work with JSON in SQLite even without this module.
此问题已在 0.7.3
中解决。