[sqlalchemy] expression inside mysql Interval statement

2009-06-19 Thread Alex K

Hi All,
I need to construct the following query:

select ADDDATE(start,INTERVAL rt_daily_days DAY) from _event where
repeat_type = 1;

and I have difficulties in dealing with the following statement:

INTERVAL rt_daily_days DAY,

how can I do this using sqlalchemy func interfaces?

func.ADDATE(Event.start, ??? Event.rt_daily_days ???)

the following

func.ADDATE(Event.start, func.INTERVAL(Event.rt_daily_days,'DAY'))

generates  INTERVAL(rt_daily_days,'DAY') what does not work for mysql

Any ideas?

Alex


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Selecting an ORM from self JOINs and/or from a query with subqueries

2009-06-19 Thread Alexander Kotelnikov

Assuming I have a table
CREATE TABLE seq (i int auto_increment primary key, used bool default
false);

And want to query an mapped object from it which corresponds a query

SELECT i,used FROM seq WHERE NOT used AND i+1 IN (SELECT i FROM seq
WHERE AND used)
or (more or less equivalent)
SELECT seq.i,seq.used FROM seq JOIN seq AS seq1 WHERE seq.i+1==seq1.i
AND NOT seq.used AND NOT seq1.used
(retrieve first of n, here - 2, not used numbers in a sequence)

Number of subqueries/JOINs might be a runtime parameter.

Would be nice to know if it is possible, and if it is - how?

Thanks,
A


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Optimizing joined entity loads

2009-06-19 Thread millerdev

  Thanks a lot for the tips on how to approach this problem. That's
  exactly what I needed.

 in 0.4 you'd get it off the impl (0.5 too, this is just uglier API):

Excellent! Here's what I came up with as an initial solution:

def poly_load(parent, collection, path):
def itersiblings(parent, path):
def iteritems(items, attr):
for item in items:
for child in getattr(item, attr):
yield child
items = [parent]
while path:
items = iteritems(items, path.pop(0))
return items
path = path.split(.)
assert len(path) % 2 == 0, path must contain an even number of
elements
mid = len(path) / 2
gparent = parent
for attr in path[:mid]:
gparent = getattr(gparent, attr)
session = sqlalchemy.orm.session.object_session(parent)
backref = getattr(type(parent), collection).property.backref.key
itemclass = getattr(type(parent),
collection).property.mapper.class_
qry = session.query(itemclass) \
.join([backref] + path[:mid]) \
.filter(type(gparent).table.c.id == gparent.id)
groups = defaultdict(list)
for item in qry:
groups[getattr(item, backref).id].append(item)
impl = getattr(type(parent), collection).impl
for sibling in itersiblings(gparent, path[mid:]):
if sibling.id in groups:
impl.set_committed_value(sibling._state, groups.get
(sibling.id))

Example usage:

# prepare for takeoff
order = session.get(Order, 123)
item = order.items[0] # triggers lazy load

# sit back and watch the fireworks!

poly_load(item, attributes, order.items)
# BOOM loaded all attributes of all items

poly_load(item, tags, order.items)
# BOOM loaded all tags of all items

poly_load(item.tags[0], bars, item.order.items.tags)
# BOOOM loaded all bars of all tags of all items

Some assumptions I was able to make that kept it simple:
- All mapped classes in my model have a 'table' attribute
- All entities in my model have an 'id' attribute, which is the
primary key.
- Relationships traversed by this loader are configured with the
necessary backrefs to make it work.

Initial tests seem to show a DRAMATIC performance improvement. Thanks
a lot for your help Mike.

Next up, roll this into a loader strategy so I can configure it on the
mapper and have it all happen automatically.

~ Daniel
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Selecting an ORM from self JOINs and/or from a query with subqueries

2009-06-19 Thread Michael Bayer

Alexander Kotelnikov wrote:

 Assuming I have a table
 CREATE TABLE seq (i int auto_increment primary key, used bool default
 false);

 And want to query an mapped object from it which corresponds a query

 SELECT i,used FROM seq WHERE NOT used AND i+1 IN (SELECT i FROM seq
 WHERE AND used)
 or (more or less equivalent)
 SELECT seq.i,seq.used FROM seq JOIN seq AS seq1 WHERE seq.i+1==seq1.i
 AND NOT seq.used AND NOT seq1.used
 (retrieve first of n, here - 2, not used numbers in a sequence)

 Number of subqueries/JOINs might be a runtime parameter.

 Would be nice to know if it is possible, and if it is - how?

you can map to any select(), but since the statement here is a runtime
thing just map to the seq table normally and use Query as needed to
construct the joins and filter criterion.   If you're looking to automate
adding N joins, just build a function that calls query.join() the
appropriate number of times.   For an example of a completely different
use case where a self-referential query.join() is being called an
arbitrary number of times, see the elementtree/optimized_al.py example in
the distribution.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Optimizing joined entity loads

2009-06-19 Thread Michael Bayer

millerdev wrote:

  Thanks a lot for the tips on how to approach this problem. That's
  exactly what I needed.

 in 0.4 you'd get it off the impl (0.5 too, this is just uglier API):

 Excellent! Here's what I came up with as an initial solution:

 def poly_load(parent, collection, path):
 def itersiblings(parent, path):
 def iteritems(items, attr):
 for item in items:
 for child in getattr(item, attr):
 yield child
 items = [parent]
 while path:
 items = iteritems(items, path.pop(0))
 return items
 path = path.split(.)
 assert len(path) % 2 == 0, path must contain an even number of
 elements
 mid = len(path) / 2
 gparent = parent
 for attr in path[:mid]:
 gparent = getattr(gparent, attr)
 session = sqlalchemy.orm.session.object_session(parent)
 backref = getattr(type(parent), collection).property.backref.key
 itemclass = getattr(type(parent),
 collection).property.mapper.class_
 qry = session.query(itemclass) \
 .join([backref] + path[:mid]) \
 .filter(type(gparent).table.c.id == gparent.id)
 groups = defaultdict(list)
 for item in qry:
 groups[getattr(item, backref).id].append(item)
 impl = getattr(type(parent), collection).impl
 for sibling in itersiblings(gparent, path[mid:]):
 if sibling.id in groups:
 impl.set_committed_value(sibling._state, groups.get
 (sibling.id))


do i see correctly the implementation relies upon the presence of a
backref  ?  thats an example of a perfectly fine restriction that is not
good enough for core ;) .



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] aggregation with count and webhelpers.paginate

2009-06-19 Thread Hollister

I have a simple query with a group_by and count(), and I'd like to
pass this to webhelpers.paginate for display:

 def referrers(self):
s = select([m.hit_table.c.id, m.hit_table.c.referer, func.count
(m.Hit.referer).label('count')],
   from_obj = [m.hit_table],
   group_by = [m.Hit.referer],
   order_by = 'count desc')

query = meta.Session.query(m.Hit).from_statement(s)

c.paginator = paginate.Page(
query,
page = int(request.params.get('page', 1)),
items_per_page = 50,
)
return render('/derived/hits/referrer_list.html')

The sql generated by this is fine:

   SELECT hit.id AS hit_id, hit.referer AS hit_referer, count
(hit.referer) AS count
   FROM hit GROUP BY hit.referer ORDER BY count desc

and the results are correct.

When I run this, I get:

Module sqlalchemy.orm.query:1956 in setup_context
  context.froms.append(self.selectable)
   if context.order_by is False and self.mapper.order_by:
   context.order_by = self.mapper.order_by
  if context.order_by is False and self.mapper.order_by:
AttributeError: 'QueryContext' object has no attribute 'order_by'

This evidently has nothing to do with the query's order_by clause
since I get the same error if I remove it. If I execute the query and
pass the results to paginate:

results = query.all()

c.paginator = paginate.Page(
results,
page = int(request.params.get('page', 1)),
items_per_page = 50,
)

I get:

AttributeError: 'Hit' object has no attribute 'count'

Which I guess makes sense, since I don't have that defined in my
class.

What am I doing wrong?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: aggregation with count and webhelpers.paginate

2009-06-19 Thread Hollister

Update:

If I run:
results = meta.Session.execute(s).fetchall()
and pass that to paginate, it works.

I guess I just needed the ResultProxy, and not a collection of mapped
objects.

Mike, if you have any additional insight for me, I would appreciate
it.


On Jun 19, 11:30 am, Hollister a.hollister.willi...@gmail.com wrote:
 I have a simple query with a group_by and count(), and I'd like to
 pass this to webhelpers.paginate for display:

  def referrers(self):
         s = select([m.hit_table.c.id, m.hit_table.c.referer, func.count
 (m.Hit.referer).label('count')],
                    from_obj = [m.hit_table],
                    group_by = [m.Hit.referer],
                    order_by = 'count desc')

         query = meta.Session.query(m.Hit).from_statement(s)

         c.paginator = paginate.Page(
             query,
             page = int(request.params.get('page', 1)),
             items_per_page = 50,
         )
         return render('/derived/hits/referrer_list.html')

 The sql generated by this is fine:

    SELECT hit.id AS hit_id, hit.referer AS hit_referer, count
 (hit.referer) AS count
    FROM hit GROUP BY hit.referer ORDER BY count desc

 and the results are correct.

 When I run this, I get:

 Module sqlalchemy.orm.query:1956 in setup_context
           context.froms.append(self.selectable)
                if context.order_by is False and self.mapper.order_by:
                    context.order_by = self.mapper.order_by  if 
 context.order_by is False and self.mapper.order_by:

 AttributeError: 'QueryContext' object has no attribute 'order_by'

 This evidently has nothing to do with the query's order_by clause
 since I get the same error if I remove it. If I execute the query and
 pass the results to paginate:

 results = query.all()

         c.paginator = paginate.Page(
             results,
             page = int(request.params.get('page', 1)),
             items_per_page = 50,
         )

 I get:

 AttributeError: 'Hit' object has no attribute 'count'

 Which I guess makes sense, since I don't have that defined in my
 class.

 What am I doing wrong?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: aggregation with count and webhelpers.paginate

2009-06-19 Thread Hollister

The bad news with results = meta.Session.execute(s).fetchall() is
that it runs for every page in the paginator, fetching all rows each
time.

Thoughts, anyone?

On Jun 19, 12:17 pm, Hollister a.hollister.willi...@gmail.com wrote:
 Update:

 If I run:
     results = meta.Session.execute(s).fetchall()
 and pass that to paginate, it works.

 I guess I just needed the ResultProxy, and not a collection of mapped
 objects.

 Mike, if you have any additional insight for me, I would appreciate
 it.

 On Jun 19, 11:30 am, Hollister a.hollister.willi...@gmail.com wrote:

  I have a simple query with a group_by and count(), and I'd like to
  pass this to webhelpers.paginate for display:

   def referrers(self):
          s = select([m.hit_table.c.id, m.hit_table.c.referer, func.count
  (m.Hit.referer).label('count')],
                     from_obj = [m.hit_table],
                     group_by = [m.Hit.referer],
                     order_by = 'count desc')

          query = meta.Session.query(m.Hit).from_statement(s)

          c.paginator = paginate.Page(
              query,
              page = int(request.params.get('page', 1)),
              items_per_page = 50,
          )
          return render('/derived/hits/referrer_list.html')

  The sql generated by this is fine:

     SELECT hit.id AS hit_id, hit.referer AS hit_referer, count
  (hit.referer) AS count
     FROM hit GROUP BY hit.referer ORDER BY count desc

  and the results are correct.

  When I run this, I get:

  Module sqlalchemy.orm.query:1956 in setup_context
            context.froms.append(self.selectable)
                 if context.order_by is False and self.mapper.order_by:
                     context.order_by = self.mapper.order_by  if 
  context.order_by is False and self.mapper.order_by:

  AttributeError: 'QueryContext' object has no attribute 'order_by'

  This evidently has nothing to do with the query's order_by clause
  since I get the same error if I remove it. If I execute the query and
  pass the results to paginate:

  results = query.all()

          c.paginator = paginate.Page(
              results,
              page = int(request.params.get('page', 1)),
              items_per_page = 50,
          )

  I get:

  AttributeError: 'Hit' object has no attribute 'count'

  Which I guess makes sense, since I don't have that defined in my
  class.

  What am I doing wrong?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: aggregation with count and webhelpers.paginate

2009-06-19 Thread Michael Bayer

Hollister wrote:

 When I run this, I get:

 Module sqlalchemy.orm.query:1956 in setup_context
           context.froms.append(self.selectable)
                if context.order_by is False and self.mapper.order_by:
                    context.order_by = self.mapper.order_by  if
 context.order_by is False and self.mapper.order_by:

I need a full stack trace on this since once from_statement() is called,
setup_context() should never be called.



 AttributeError: 'QueryContext' object has no attribute 'order_by'

 This evidently has nothing to do with the query's order_by clause
 since I get the same error if I remove it. If I execute the query and
 pass the results to paginate:

 results = query.all()

         c.paginator = paginate.Page(
             results,
             page = int(request.params.get('page', 1)),
             items_per_page = 50,
         )

 I get:

 AttributeError: 'Hit' object has no attribute 'count'

 Which I guess makes sense, since I don't have that defined in my
 class.

 What am I doing wrong?
 



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: aggregation with count and webhelpers.paginate

2009-06-19 Thread Michael Bayer

oh.  if the paginator uses slices to provide LIMIT/OFFSET, you need to
construct a Query that can be limited via slice.  don't use select() here,
use Query fully.



Hollister wrote:

 The bad news with results = meta.Session.execute(s).fetchall() is
 that it runs for every page in the paginator, fetching all rows each
 time.

 Thoughts, anyone?

 On Jun 19, 12:17 pm, Hollister a.hollister.willi...@gmail.com wrote:
 Update:

 If I run:
     results = meta.Session.execute(s).fetchall()
 and pass that to paginate, it works.

 I guess I just needed the ResultProxy, and not a collection of mapped
 objects.

 Mike, if you have any additional insight for me, I would appreciate
 it.

 On Jun 19, 11:30 am, Hollister a.hollister.willi...@gmail.com wrote:

  I have a simple query with a group_by and count(), and I'd like to
  pass this to webhelpers.paginate for display:

   def referrers(self):
          s = select([m.hit_table.c.id, m.hit_table.c.referer,
 func.count
  (m.Hit.referer).label('count')],
                     from_obj = [m.hit_table],
                     group_by = [m.Hit.referer],
                     order_by = 'count desc')

          query = meta.Session.query(m.Hit).from_statement(s)

          c.paginator = paginate.Page(
              query,
              page = int(request.params.get('page', 1)),
              items_per_page = 50,
          )
          return render('/derived/hits/referrer_list.html')

  The sql generated by this is fine:

     SELECT hit.id AS hit_id, hit.referer AS hit_referer, count
  (hit.referer) AS count
     FROM hit GROUP BY hit.referer ORDER BY count desc

  and the results are correct.

  When I run this, I get:

  Module sqlalchemy.orm.query:1956 in setup_context
            context.froms.append(self.selectable)
                 if context.order_by is False and self.mapper.order_by:
                     context.order_by = self.mapper.order_by  if
 context.order_by is False and self.mapper.order_by:

  AttributeError: 'QueryContext' object has no attribute 'order_by'

  This evidently has nothing to do with the query's order_by clause
  since I get the same error if I remove it. If I execute the query and
  pass the results to paginate:

  results = query.all()

          c.paginator = paginate.Page(
              results,
              page = int(request.params.get('page', 1)),
              items_per_page = 50,
          )

  I get:

  AttributeError: 'Hit' object has no attribute 'count'

  Which I guess makes sense, since I don't have that defined in my
  class.

  What am I doing wrong?


 



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: aggregation with count and webhelpers.paginate

2009-06-19 Thread Hollister

Here you go:

URL: http://dev:5000/hits/referrers
File '/home/aw/venv/dev/lib/python2.6/site-packages/WebError-0.10.1-
py2.6.egg/weberror/evalexception.py', line 431 in respond
  app_iter = self.application(environ, detect_start_response)
File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3-
py2.6.egg/beaker/middleware.py', line 81 in __call__
  return self.app(environ, start_response)
File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3-
py2.6.egg/beaker/middleware.py', line 160 in __call__
  return self.wrap_app(environ, session_start_response)
File '/home/aw/venv/dev/lib/python2.6/site-packages/Routes-1.10.3-
py2.6.egg/routes/middleware.py', line 130 in __call__
  response = self.app(environ, start_response)
File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
py2.6.egg/pylons/wsgiapp.py', line 125 in __call__
  response = self.dispatch(controller, environ, start_response)
File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
py2.6.egg/pylons/wsgiapp.py', line 324 in dispatch
  return controller(environ, start_response)
File '/home/aw/projects/resonate/corp/corp/lib/base.py', line 18 in
__call__
  return WSGIController.__call__(self, environ, start_response)
File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
py2.6.egg/pylons/controllers/core.py', line 221 in __call__
  response = self._dispatch_call()
File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
py2.6.egg/pylons/controllers/core.py', line 172 in _dispatch_call
  response = self._inspect_call(func)
File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
py2.6.egg/pylons/controllers/core.py', line 107 in _inspect_call
  result = self._perform_call(func, args)
File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
py2.6.egg/pylons/controllers/core.py', line 60 in _perform_call
  return func(**args)
File '/home/aw/projects/resonate/corp/corp/controllers/hits.py', line
67 in referrers
  items_per_page = 50,
File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4-
py2.6.egg/webhelpers/paginate.py', line 333 in __init__
  self.item_count = len(self.collection)
File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4-
py2.6.egg/webhelpers/paginate.py', line 204 in __len__
  return self.obj.count()
File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2-
py2.6.egg/sqlalchemy/orm/query.py', line 1465 in count
  should_nest = should_nest[0]
File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2-
py2.6.egg/sqlalchemy/orm/query.py', line 1472 in _col_aggregate
  entity.setup_context(self, context)
File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2-
py2.6.egg/sqlalchemy/orm/query.py', line 1956 in setup_context
  if context.order_by is False and self.mapper.order_by:
AttributeError: 'QueryContext' object has no attribute 'order_by'

On Jun 19, 12:35 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 Hollister wrote:

  When I run this, I get:

  Module sqlalchemy.orm.query:1956 in setup_context
            context.froms.append(self.selectable)
                 if context.order_by is False and self.mapper.order_by:
                     context.order_by = self.mapper.order_by  if
  context.order_by is False and self.mapper.order_by:

 I need a full stack trace on this since once from_statement() is called,
 setup_context() should never be called.



  AttributeError: 'QueryContext' object has no attribute 'order_by'

  This evidently has nothing to do with the query's order_by clause
  since I get the same error if I remove it. If I execute the query and
  pass the results to paginate:

  results = query.all()

          c.paginator = paginate.Page(
              results,
              page = int(request.params.get('page', 1)),
              items_per_page = 50,
          )

  I get:

  AttributeError: 'Hit' object has no attribute 'count'

  Which I guess makes sense, since I don't have that defined in my
  class.

  What am I doing wrong?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: aggregation with count and webhelpers.paginate

2009-06-19 Thread Michael Bayer

you can't call count() when you've used from_statement, that should be
raising an error.   the bug is that no error is being raised.

Hollister wrote:

 Here you go:

 URL: http://dev:5000/hits/referrers
 File '/home/aw/venv/dev/lib/python2.6/site-packages/WebError-0.10.1-
 py2.6.egg/weberror/evalexception.py', line 431 in respond
   app_iter = self.application(environ, detect_start_response)
 File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3-
 py2.6.egg/beaker/middleware.py', line 81 in __call__
   return self.app(environ, start_response)
 File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3-
 py2.6.egg/beaker/middleware.py', line 160 in __call__
   return self.wrap_app(environ, session_start_response)
 File '/home/aw/venv/dev/lib/python2.6/site-packages/Routes-1.10.3-
 py2.6.egg/routes/middleware.py', line 130 in __call__
   response = self.app(environ, start_response)
 File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
 py2.6.egg/pylons/wsgiapp.py', line 125 in __call__
   response = self.dispatch(controller, environ, start_response)
 File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
 py2.6.egg/pylons/wsgiapp.py', line 324 in dispatch
   return controller(environ, start_response)
 File '/home/aw/projects/resonate/corp/corp/lib/base.py', line 18 in
 __call__
   return WSGIController.__call__(self, environ, start_response)
 File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
 py2.6.egg/pylons/controllers/core.py', line 221 in __call__
   response = self._dispatch_call()
 File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
 py2.6.egg/pylons/controllers/core.py', line 172 in _dispatch_call
   response = self._inspect_call(func)
 File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
 py2.6.egg/pylons/controllers/core.py', line 107 in _inspect_call
   result = self._perform_call(func, args)
 File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
 py2.6.egg/pylons/controllers/core.py', line 60 in _perform_call
   return func(**args)
 File '/home/aw/projects/resonate/corp/corp/controllers/hits.py', line
 67 in referrers
   items_per_page = 50,
 File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4-
 py2.6.egg/webhelpers/paginate.py', line 333 in __init__
   self.item_count = len(self.collection)
 File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4-
 py2.6.egg/webhelpers/paginate.py', line 204 in __len__
   return self.obj.count()
 File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2-
 py2.6.egg/sqlalchemy/orm/query.py', line 1465 in count
   should_nest = should_nest[0]
 File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2-
 py2.6.egg/sqlalchemy/orm/query.py', line 1472 in _col_aggregate
   entity.setup_context(self, context)
 File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2-
 py2.6.egg/sqlalchemy/orm/query.py', line 1956 in setup_context
   if context.order_by is False and self.mapper.order_by:
 AttributeError: 'QueryContext' object has no attribute 'order_by'

 On Jun 19, 12:35 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 Hollister wrote:

  When I run this, I get:

  Module sqlalchemy.orm.query:1956 in setup_context
            context.froms.append(self.selectable)
                 if context.order_by is False and self.mapper.order_by:
                     context.order_by = self.mapper.order_by  if
  context.order_by is False and self.mapper.order_by:

 I need a full stack trace on this since once from_statement() is called,
 setup_context() should never be called.



  AttributeError: 'QueryContext' object has no attribute 'order_by'

  This evidently has nothing to do with the query's order_by clause
  since I get the same error if I remove it. If I execute the query and
  pass the results to paginate:

  results = query.all()

          c.paginator = paginate.Page(
              results,
              page = int(request.params.get('page', 1)),
              items_per_page = 50,
          )

  I get:

  AttributeError: 'Hit' object has no attribute 'count'

  Which I guess makes sense, since I don't have that defined in my
  class.

  What am I doing wrong?


 



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: aggregation with count and webhelpers.paginate

2009-06-19 Thread Hollister

Ok, I see.
So what's the best way for me to construct and execute this query?

On Jun 19, 1:30 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 you can't call count() when you've used from_statement, that should be
 raising an error.   the bug is that no error is being raised.

 Hollister wrote:

  Here you go:

  URL:http://dev:5000/hits/referrers
  File '/home/aw/venv/dev/lib/python2.6/site-packages/WebError-0.10.1-
  py2.6.egg/weberror/evalexception.py', line 431 in respond
    app_iter = self.application(environ, detect_start_response)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3-
  py2.6.egg/beaker/middleware.py', line 81 in __call__
    return self.app(environ, start_response)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3-
  py2.6.egg/beaker/middleware.py', line 160 in __call__
    return self.wrap_app(environ, session_start_response)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Routes-1.10.3-
  py2.6.egg/routes/middleware.py', line 130 in __call__
    response = self.app(environ, start_response)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
  py2.6.egg/pylons/wsgiapp.py', line 125 in __call__
    response = self.dispatch(controller, environ, start_response)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
  py2.6.egg/pylons/wsgiapp.py', line 324 in dispatch
    return controller(environ, start_response)
  File '/home/aw/projects/resonate/corp/corp/lib/base.py', line 18 in
  __call__
    return WSGIController.__call__(self, environ, start_response)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
  py2.6.egg/pylons/controllers/core.py', line 221 in __call__
    response = self._dispatch_call()
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
  py2.6.egg/pylons/controllers/core.py', line 172 in _dispatch_call
    response = self._inspect_call(func)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
  py2.6.egg/pylons/controllers/core.py', line 107 in _inspect_call
    result = self._perform_call(func, args)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
  py2.6.egg/pylons/controllers/core.py', line 60 in _perform_call
    return func(**args)
  File '/home/aw/projects/resonate/corp/corp/controllers/hits.py', line
  67 in referrers
    items_per_page = 50,
  File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4-
  py2.6.egg/webhelpers/paginate.py', line 333 in __init__
    self.item_count = len(self.collection)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4-
  py2.6.egg/webhelpers/paginate.py', line 204 in __len__
    return self.obj.count()
  File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2-
  py2.6.egg/sqlalchemy/orm/query.py', line 1465 in count
    should_nest = should_nest[0]
  File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2-
  py2.6.egg/sqlalchemy/orm/query.py', line 1472 in _col_aggregate
    entity.setup_context(self, context)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2-
  py2.6.egg/sqlalchemy/orm/query.py', line 1956 in setup_context
    if context.order_by is False and self.mapper.order_by:
  AttributeError: 'QueryContext' object has no attribute 'order_by'

  On Jun 19, 12:35 pm, Michael Bayer mike...@zzzcomputing.com wrote:
  Hollister wrote:

   When I run this, I get:

   Module sqlalchemy.orm.query:1956 in setup_context
         context.froms.append(self.selectable)
                  if context.order_by is False and self.mapper.order_by:
                      context.order_by = self.mapper.order_by  if
   context.order_by is False and self.mapper.order_by:

  I need a full stack trace on this since once from_statement() is called,
  setup_context() should never be called.

   AttributeError: 'QueryContext' object has no attribute 'order_by'

   This evidently has nothing to do with the query's order_by clause
   since I get the same error if I remove it. If I execute the query and
   pass the results to paginate:

   results = query.all()

           c.paginator = paginate.Page(
               results,
               page = int(request.params.get('page', 1)),
               items_per_page = 50,
           )

   I get:

   AttributeError: 'Hit' object has no attribute 'count'

   Which I guess makes sense, since I don't have that defined in my
   class.

   What am I doing wrong?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Selecting an ORM from self JOINs and/or from a query with subqueries

2009-06-19 Thread sacha

Michael,

do you mean, that subqueries could not be wrapped into sqlalchemy?
what should be the arguments to join() I failed to figure out? how do
I reference different instances of seq in fileter() after?

Thanks,
A

On Jun 19, 7:43 am, Michael Bayer mike...@zzzcomputing.com wrote:

 you can map to any select(), but since the statement here is a runtime
 thing just map to the seq table normally and use Query as needed to
 construct the joins and filter criterion.   If you're looking to automate
 adding N joins, just build a function that calls query.join() the
 appropriate number of times.   For an example of a completely different
 use case where a self-referential query.join() is being called an
 arbitrary number of times, see the elementtree/optimized_al.py example in
 the distribution.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: aggregation with count and webhelpers.paginate

2009-06-19 Thread Michael Bayer

Hollister wrote:

 Ok, I see.
 So what's the best way for me to construct and execute this query?

use Query:

session.query(MyClass.someid, 
MyClass.somethingelse).filter(..whatever..).order_by(..whatever...)




 On Jun 19, 1:30 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 you can't call count() when you've used from_statement, that should be
 raising an error.   the bug is that no error is being raised.

 Hollister wrote:

  Here you go:

  URL:http://dev:5000/hits/referrers
  File '/home/aw/venv/dev/lib/python2.6/site-packages/WebError-0.10.1-
  py2.6.egg/weberror/evalexception.py', line 431 in respond
    app_iter = self.application(environ, detect_start_response)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3-
  py2.6.egg/beaker/middleware.py', line 81 in __call__
    return self.app(environ, start_response)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3-
  py2.6.egg/beaker/middleware.py', line 160 in __call__
    return self.wrap_app(environ, session_start_response)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Routes-1.10.3-
  py2.6.egg/routes/middleware.py', line 130 in __call__
    response = self.app(environ, start_response)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
  py2.6.egg/pylons/wsgiapp.py', line 125 in __call__
    response = self.dispatch(controller, environ, start_response)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
  py2.6.egg/pylons/wsgiapp.py', line 324 in dispatch
    return controller(environ, start_response)
  File '/home/aw/projects/resonate/corp/corp/lib/base.py', line 18 in
  __call__
    return WSGIController.__call__(self, environ, start_response)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
  py2.6.egg/pylons/controllers/core.py', line 221 in __call__
    response = self._dispatch_call()
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
  py2.6.egg/pylons/controllers/core.py', line 172 in _dispatch_call
    response = self._inspect_call(func)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
  py2.6.egg/pylons/controllers/core.py', line 107 in _inspect_call
    result = self._perform_call(func, args)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
  py2.6.egg/pylons/controllers/core.py', line 60 in _perform_call
    return func(**args)
  File '/home/aw/projects/resonate/corp/corp/controllers/hits.py', line
  67 in referrers
    items_per_page = 50,
  File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4-
  py2.6.egg/webhelpers/paginate.py', line 333 in __init__
    self.item_count = len(self.collection)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4-
  py2.6.egg/webhelpers/paginate.py', line 204 in __len__
    return self.obj.count()
  File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2-
  py2.6.egg/sqlalchemy/orm/query.py', line 1465 in count
    should_nest = should_nest[0]
  File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2-
  py2.6.egg/sqlalchemy/orm/query.py', line 1472 in _col_aggregate
    entity.setup_context(self, context)
  File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2-
  py2.6.egg/sqlalchemy/orm/query.py', line 1956 in setup_context
    if context.order_by is False and self.mapper.order_by:
  AttributeError: 'QueryContext' object has no attribute 'order_by'

  On Jun 19, 12:35 pm, Michael Bayer mike...@zzzcomputing.com wrote:
  Hollister wrote:

   When I run this, I get:

   Module sqlalchemy.orm.query:1956 in setup_context
         context.froms.append(self.selectable)
                  if context.order_by is False and
 self.mapper.order_by:
                      context.order_by = self.mapper.order_by  if
   context.order_by is False and self.mapper.order_by:

  I need a full stack trace on this since once from_statement() is
 called,
  setup_context() should never be called.

   AttributeError: 'QueryContext' object has no attribute 'order_by'

   This evidently has nothing to do with the query's order_by clause
   since I get the same error if I remove it. If I execute the query
 and
   pass the results to paginate:

   results = query.all()

           c.paginator = paginate.Page(
               results,
               page = int(request.params.get('page', 1)),
               items_per_page = 50,
           )

   I get:

   AttributeError: 'Hit' object has no attribute 'count'

   Which I guess makes sense, since I don't have that defined in my
   class.

   What am I doing wrong?


 



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en

[sqlalchemy] Re: Selecting an ORM from self JOINs and/or from a query with subqueries

2009-06-19 Thread Michael Bayer

sacha wrote:

 Michael,

 do you mean, that subqueries could not be wrapped into sqlalchemy?

you talked about mapping to a select statement.  mapping means this:

m = mapper(MyClass, someselectable)

mapping like the above is usually done against individual tables, and
usually once per class per application.  You can do it against select()
statements but this is usually unnecessary.  You can also make multiple
mappers for a single class in an ad-hoc way, but again this is an ancient
use case that is much better addressed by using the Query object as
needed.


 what should be the arguments to join() I failed to figure out? how do
 I reference different instances of seq in fileter() after?


Usually you use query.join().   Self referential queries require an alias
for each join target.  There is an example at
http://www.sqlalchemy.org/docs/05/ormtutorial.html#using-aliases .  Also
the example I mentioned earlier in examples/elementtree/optimized_ai.py
shows exactly a dynamically-constructed self-referential join.


 Thanks,
 A

 On Jun 19, 7:43 am, Michael Bayer mike...@zzzcomputing.com wrote:

 you can map to any select(), but since the statement here is a runtime
 thing just map to the seq table normally and use Query as needed to
 construct the joins and filter criterion.   If you're looking to
 automate
 adding N joins, just build a function that calls query.join() the
 appropriate number of times.   For an example of a completely different
 use case where a self-referential query.join() is being called an
 arbitrary number of times, see the elementtree/optimized_al.py example
 in
 the distribution.
 



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Selecting an ORM from self JOINs and/or from a query with subqueries

2009-06-19 Thread sacha

I really want to get a clear vision.

So, I have a table x and mapped class X.
I can use query(X) for simple queries, can I query(X) for structured
ones like
SELECT * FROM x WHERE x.a IN (SELECT )
?

Same about multi-cartesian product can I use query(X).join()
for SELECT * FROM x JOIN x JOIN x .
?

All examples for the latter involve additional tables.


On Jun 19, 12:03 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 sacha wrote:

  Michael,

  do you mean, that subqueries could not be wrapped into sqlalchemy?

 you talked about mapping to a select statement.  mapping means this:

 m = mapper(MyClass, someselectable)

 mapping like the above is usually done against individual tables, and
 usually once per class per application.  You can do it against select()
 statements but this is usually unnecessary.  You can also make multiple
 mappers for a single class in an ad-hoc way, but again this is an ancient
 use case that is much better addressed by using the Query object as
 needed.

  what should be the arguments to join() I failed to figure out? how do
  I reference different instances of seq in fileter() after?

 Usually you use query.join().   Self referential queries require an alias
 for each join target.  There is an example 
 athttp://www.sqlalchemy.org/docs/05/ormtutorial.html#using-aliases.  Also
 the example I mentioned earlier in examples/elementtree/optimized_ai.py
 shows exactly a dynamically-constructed self-referential join.



  Thanks,
  A

  On Jun 19, 7:43 am, Michael Bayer mike...@zzzcomputing.com wrote:

  you can map to any select(), but since the statement here is a runtime
  thing just map to the seq table normally and use Query as needed to
  construct the joins and filter criterion.   If you're looking to
  automate
  adding N joins, just build a function that calls query.join() the
  appropriate number of times.   For an example of a completely different
  use case where a self-referential query.join() is being called an
  arbitrary number of times, see the elementtree/optimized_al.py example
  in
  the distribution.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Selecting an ORM from self JOINs and/or from a query with subqueries

2009-06-19 Thread sacha

I meant SELECT x.* FROM x JOIN x AS x1 JOIN x AS x2 ... WHERE

On Jun 19, 1:55 pm, sacha sa...@myxomop.com wrote:

 Same about multi-cartesian product can I use query(X).join()
 for SELECT * FROM x JOIN x JOIN x .
 ?

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Selecting an ORM from self JOINs and/or from a query with subqueries

2009-06-19 Thread Michael Bayer

sacha wrote:

 I really want to get a clear vision.

 So, I have a table x and mapped class X.
 I can use query(X) for simple queries, can I query(X) for structured
 ones like
 SELECT * FROM x WHERE x.a IN (SELECT )


sel = session.query(X.a).filter(X.b=='foo')

session.query(X).filter(X.a.in_(sel))


 Same about multi-cartesian product can I use query(X).join()
 for SELECT * FROM x JOIN x JOIN x .
 ?


for self-referential, use aliases as follows:


x1 = aliased(X)
x2 = aliased(X)
x3 = ...

session.query(X).join((x1, X.somerelation), (x2, x1.somerelation), ...)

or

session.query(X).join((x1, X.some_id==x1.some_other_id), (x2,
x1.some_id==x2.some_other_id), ...)

anything can be in query() too, i.e. query(X, x1, x2.foo, ...)

then

.filter(X.foo=='bar').filter(x1.bar=='bat').filter(x2.hoho=='lala')

etc.








 On Jun 19, 12:03 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 sacha wrote:

  Michael,

  do you mean, that subqueries could not be wrapped into sqlalchemy?

 you talked about mapping to a select statement.  mapping means this:

 m = mapper(MyClass, someselectable)

 mapping like the above is usually done against individual tables, and
 usually once per class per application.  You can do it against select()
 statements but this is usually unnecessary.  You can also make multiple
 mappers for a single class in an ad-hoc way, but again this is an
 ancient
 use case that is much better addressed by using the Query object as
 needed.

  what should be the arguments to join() I failed to figure out? how do
  I reference different instances of seq in fileter() after?

 Usually you use query.join().   Self referential queries require an
 alias
 for each join target.  There is an example
 athttp://www.sqlalchemy.org/docs/05/ormtutorial.html#using-aliases.
  Also
 the example I mentioned earlier in examples/elementtree/optimized_ai.py
 shows exactly a dynamically-constructed self-referential join.



  Thanks,
  A

  On Jun 19, 7:43 am, Michael Bayer mike...@zzzcomputing.com wrote:

  you can map to any select(), but since the statement here is a
 runtime
  thing just map to the seq table normally and use Query as needed to
  construct the joins and filter criterion.   If you're looking to
  automate
  adding N joins, just build a function that calls query.join() the
  appropriate number of times.   For an example of a completely
 different
  use case where a self-referential query.join() is being called an
  arbitrary number of times, see the elementtree/optimized_al.py
 example
  in
  the distribution.
 



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: aggregation with count and webhelpers.paginate

2009-06-19 Thread Hollister

Well, that worked great:

q = meta.Session.query(m.Hit.referer, func.count(m.Hit.id))\
.group_by(m.Hit.referer)\
.order_by(func.count(m.Hit.id).desc())

Thanks!

ps: Is there a better way to specify the count in the order_by?

On Jun 19, 2:58 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 Hollister wrote:

  Ok, I see.
  So what's the best way for me to construct and execute this query?

 use Query:

 session.query(MyClass.someid,
 MyClass.somethingelse).filter(..whatever..).order_by(..whatever...)



  On Jun 19, 1:30 pm, Michael Bayer mike...@zzzcomputing.com wrote:
  you can't call count() when you've used from_statement, that should be
  raising an error.   the bug is that no error is being raised.

  Hollister wrote:

   Here you go:

   URL:http://dev:5000/hits/referrers
   File '/home/aw/venv/dev/lib/python2.6/site-packages/WebError-0.10.1-
   py2.6.egg/weberror/evalexception.py', line 431 in respond
     app_iter = self.application(environ, detect_start_response)
   File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3-
   py2.6.egg/beaker/middleware.py', line 81 in __call__
     return self.app(environ, start_response)
   File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3-
   py2.6.egg/beaker/middleware.py', line 160 in __call__
     return self.wrap_app(environ, session_start_response)
   File '/home/aw/venv/dev/lib/python2.6/site-packages/Routes-1.10.3-
   py2.6.egg/routes/middleware.py', line 130 in __call__
     response = self.app(environ, start_response)
   File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
   py2.6.egg/pylons/wsgiapp.py', line 125 in __call__
     response = self.dispatch(controller, environ, start_response)
   File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
   py2.6.egg/pylons/wsgiapp.py', line 324 in dispatch
     return controller(environ, start_response)
   File '/home/aw/projects/resonate/corp/corp/lib/base.py', line 18 in
   __call__
     return WSGIController.__call__(self, environ, start_response)
   File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
   py2.6.egg/pylons/controllers/core.py', line 221 in __call__
     response = self._dispatch_call()
   File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
   py2.6.egg/pylons/controllers/core.py', line 172 in _dispatch_call
     response = self._inspect_call(func)
   File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
   py2.6.egg/pylons/controllers/core.py', line 107 in _inspect_call
     result = self._perform_call(func, args)
   File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7-
   py2.6.egg/pylons/controllers/core.py', line 60 in _perform_call
     return func(**args)
   File '/home/aw/projects/resonate/corp/corp/controllers/hits.py', line
   67 in referrers
     items_per_page = 50,
   File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4-
   py2.6.egg/webhelpers/paginate.py', line 333 in __init__
     self.item_count = len(self.collection)
   File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4-
   py2.6.egg/webhelpers/paginate.py', line 204 in __len__
     return self.obj.count()
   File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2-
   py2.6.egg/sqlalchemy/orm/query.py', line 1465 in count
     should_nest = should_nest[0]
   File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2-
   py2.6.egg/sqlalchemy/orm/query.py', line 1472 in _col_aggregate
     entity.setup_context(self, context)
   File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2-
   py2.6.egg/sqlalchemy/orm/query.py', line 1956 in setup_context
     if context.order_by is False and self.mapper.order_by:
   AttributeError: 'QueryContext' object has no attribute 'order_by'

   On Jun 19, 12:35 pm, Michael Bayer mike...@zzzcomputing.com wrote:
   Hollister wrote:

When I run this, I get:

Module sqlalchemy.orm.query:1956 in setup_context
          context.froms.append(self.selectable)
               if context.order_by is False and
  self.mapper.order_by:
                   context.order_by = self.mapper.order_by  if
context.order_by is False and self.mapper.order_by:

   I need a full stack trace on this since once from_statement() is
  called,
   setup_context() should never be called.

AttributeError: 'QueryContext' object has no attribute 'order_by'

This evidently has nothing to do with the query's order_by clause
since I get the same error if I remove it. If I execute the query
  and
pass the results to paginate:

results = query.all()

        c.paginator = paginate.Page(
            results,
            page = int(request.params.get('page', 1)),
            items_per_page = 50,
        )

I get:

AttributeError: 'Hit' object has no attribute 'count'

Which I guess makes sense, since I don't have that defined in my
class.

What am I doing wrong?