Incorrect caching when slicing the same query multiple times #168

andreymal opened this Issue Feb 26, 2016 · 3 comments


None yet

3 participants

from pony.orm import *

db = Database()

class TestPost(db.Entity):
    name = Required(str, 128, autostrip=False)

db.bind('sqlite', ':memory:')
# sql_debug(True)

with db_session:
    for i in range(1, 101):
        post = TestPost(name='post {}'.format(i))

with db_session:
    query =
    print(query[30:33])  # [31, 32, 33]

with db_session:
    query =
    len(query)  # 100
    # Offopic: how about to make query.__len__() equivalent of query.count()?
    print(query[30:33])  # [1, 2, ..., 99, 100]???

with db_session:
    query =
    print(query[95:98])  # [96, 97, 98]
    print(query[30:33])  # [96, 97, 98]???
@kozlovsky kozlovsky added the bug label Feb 26, 2016
@kozlovsky kozlovsky self-assigned this Feb 26, 2016
@kozlovsky kozlovsky added this to the 0.6.5 milestone Feb 27, 2016
@kozlovsky kozlovsky closed this in e63ba66 Feb 27, 2016

Fixed. Regarding making query.__len__() equivalent of query.count(): currect behavior of query.__len__() is more efficient if you need to do iteration over resulted objects, one less query is sent to the database. Also, count() does not guarantee that the number will actually match the number of objects, because most databases does not provide serializable isolation level by default, and another transaction can insert/delete rows after the count is executed..


Так и не понял в чём именно баг (когда проявляется)


@socketpair the bug manifested itself when a different slicing was applied to the same query instance. The result of first slicing was cached and used for all subsequent slices. The correct behavior is to issue another SQL query with different LIMIT...OFFSET parameters.

@kozlovsky kozlovsky changed the title from Too many caching in core.Query? to Incorrect caching when slicing the same query multiple times Apr 4, 2016
@kozlovsky kozlovsky added a commit that referenced this issue Apr 4, 2016
@kozlovsky kozlovsky Pony ORM Release 0.6.5 (2016-04-04)
# Improvements

* Fixes #172: Query prefetch() method should load specified lazy attributes right in the main query if possible

# Bugfixes

* Fixes #168: Incorrect caching when slicing the same query multiple times
* Fixes #169: When py_check() returns False, Pony should truncate too large values in resulting ValueError message
* Fixes #171: AssertionError when saving changes of multiple objects
* Fixes #176: Autostripped strings are not validated correctly for Required attributes

See blog post for more detailed information:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment