0

在下面的示例中,当我再次调用 School.get(1) 时,学校模型有一个唯一的字段“key”。不会有任何查询,这很好。但是当调用 School.get_by_key('gxu') 查询调用时。

我想要的是,因为“key”是独一无二的,所以 SQLALchemy 可以聪明地不再查询吗?

In [3]: school = School.get(1)
INFO-2013-06-14 18:08:17,903:BEGIN (implicit)
INFO-2013-06-14 18:08:17,904:SELECT school_info._id AS school_info__id, school_info."key" AS school_info_key, school_info.name AS school_info_name 
FROM school_info 
WHERE school_info._id = ?
INFO-2013-06-14 18:08:17,905:(1,)
DEBUG-2013-06-14 18:08:17,906:Col ('school_info__id', 'school_info_key', 'school_info_name')
DEBUG-2013-06-14 18:08:17,906:Row (1, u'gxu', u'\u5e7f\u897f\u5927\u5b66')

In [4]: gxu_school = School.get_by_key('gxu')
INFO-2013-06-14 18:08:32,042:SELECT school_info._id AS school_info__id, school_info."key" AS school_info_key, school_info.name AS school_info_name 
FROM school_info 
WHERE school_info."key" = ?
LIMIT ? OFFSET ?
INFO-2013-06-14 18:08:32,045:(u'gxu', 1, 0)
DEBUG-2013-06-14 18:08:32,046:Col ('school_info__id', 'school_info_key', 'school_info_name')
DEBUG-2013-06-14 18:08:32,047:Row (1, u'gxu', u'\u5e7f\u897f\u5927\u5b66')

In [5]: 
4

1 回答 1

0

首先,我认为 get_by_key() 是 0.3 或其他一些古老的方法?是时候升级了。

接下来,对于任意唯一字段上的“缓存”,您可以尝试一个方法:UniqueObject

或者,如果您感觉更有野心,您可以使用真正的缓存设置

于 2013-06-17T05:06:19.223 回答