Christoph Haas <email-Nf+wZpSdgwd6//[EMAIL PROTECTED]> writes:

> On Saturday 03 February 2007 08:01, dds wrote:
>> Thanks for looking into the pagination stuff.
>
> No hope too soon. :)
>
>> On Jan 29, 11:54 pm, Christoph Haas <e...-Nf+wZpSdgwd6//[EMAIL PROTECTED]> 
>> wrote:
>> > The documentation
>> > athttp://pylonshq.com/WebHelpers/module-webhelpers.pagination.htmlcont
>> >ains this example:
>> >
>> >   # In this case, Person is a SQLObject class, or it could be a
>> > list/tuple person_paginator, person_set = paginate(Person, page=1)
>> >
>> > It wasn't very clear to me how that is supposed to work with
>> > SQLAlchemy. It must look like this:
>> >
>> >   person_paginator, person_set = \
>> >     h.pagination.paginate(model.Person.select())
>> >
>> > Can we please add that example to the the webhelpers/pagination/orm.py
>> > module?
>>
>> But this is exactly what the paginator is supposed to avoid when using
>> an ORM. You're selecting every person from the database, pulling it
>> into memory, and making the paginator chunk it up.
>
> Are you sure? In the background the orm.py is creating a lazy mapping that 
> is only requesting rows from the database when they are needed. As far as 
> I understand even that .select() doesn't pull anything unless you read 
> from it. And if you read just a slice the database would *then* start a 
> query with a certain LIMIT.

In the line of code above, your model.Person.select() runs
first and selects all of the persons from the database, passing
a regular collection to the paginator, thus bypassing any of
the ORM related stuff in it. There is no lazy mapping being
created. That's _not_ how it's supposed to work. On that point,
yes, I'm quite sure.

>> The point of giving it a real query object is that it should
>> use limit and offset to select only the elements from the DB
>> to make the current page, not carry it all around in memory.
>
> True, but isn't that also true for .select()? I'm really not sure because 
> I'm a newbie to SQLAlchemy. Of course a "nakes .select()" is bullshit 
> because it does not contain any selection criteria.
>
> By the way... does anyone know if there's an easier way to create a query 
> than the long prayer version: model.session_context.current.query() ?

I think you're mistaking giving it "model.Person.select", the
function, and "model.Person.select()", the result of the
function. It's the same as if you ran that in the terminal; as
you said, without selection criteria, and so it selects
everything.

Regarding queries, you don't need them when you're using
assign_mapper but keep in mind that you can call your
'session_context' variable anything you like, so you might find
the above more readable if you write it as
model.ctx.current.query , maybe :)

Anyway, I sent up a patch to fix the paginator. Try it
out. With the patch, you send the paginator anything with count
and select methods, like model.Person or a select and where
clause.

Happy hacking,
-- 
  David D. Smith

Attachment: pgpCjkDwxtYLz.pgp
Description: PGP signature

Reply via email to