[sqlalchemy] Re: Getting useful error messages

2010-02-25 Thread Hollister
Ok...maybe I'm missing something, but why wasn't that raised in this
case instead of the rather cryptic exception?

On Feb 18, 4:59 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 On Feb 18, 2010, at 1:16 PM, Hollister wrote:

  I ran into this also and it was caused by forgetting to map the class
  to the table.
  Make sure you have a line like:

  orm.mapper(TaskAction, taskaction_table)

  Mike: should there be a more specific error message for a missing
  mapping?

 there's a very specific error for that its sqlalchemy.orm.exc.UnmappedError.



  -aw

  On Feb 3, 11:04 am, Michael Bayer mike...@zzzcomputing.com wrote:
  King Simon-NFHD78 wrote:

  The line below the one you're complaining about is telling you what the
  column in question is:

    Invalid column expression 'class '__main__.TaskAction''

  So somehow, you've passed your TaskAction class in a place where SA is
  expecting a column expression. I think we'd need to see the command that
  you actually typed in to work out what the problem is.

  Also, again, please upgrade to 0.5.8.   Hundreds of bugs and sub-optimal
  behaviors have been fixed since 0.5.4p2 so you may get better results.

  Simon

  --
  You received this message because you are subscribed to the Google Groups
  sqlalchemy group.
  To post to this group, send email to sqlalch...@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.

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



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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: Getting useful error messages

2010-02-18 Thread Hollister
I ran into this also and it was caused by forgetting to map the class
to the table.
Make sure you have a line like:

orm.mapper(TaskAction, taskaction_table)

Mike: should there be a more specific error message for a missing
mapping?

-aw


On Feb 3, 11:04 am, Michael Bayer mike...@zzzcomputing.com wrote:
 King Simon-NFHD78 wrote:

  The line below the one you're complaining about is telling you what the
  column in question is:

    Invalid column expression 'class '__main__.TaskAction''

  So somehow, you've passed your TaskAction class in a place where SA is
  expecting a column expression. I think we'd need to see the command that
  you actually typed in to work out what the problem is.

 Also, again, please upgrade to 0.5.8.   Hundreds of bugs and sub-optimal
 behaviors have been fixed since 0.5.4p2 so you may get better results.



  Simon

  --
  You received this message because you are subscribed to the Google Groups
  sqlalchemy group.
  To post to this group, send email to sqlalch...@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.



-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@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: Advice on modeling a many-to-many relationship

2009-08-06 Thread Hollister

Still having a little trouble...here are the relevant mappings:

orm.mapper(Keyphrase, keyphrase_table, properties = {
'message':orm.relation(Message, backref='keyphrase'),
'campaign':orm.relation(Campaign, backref='keyphrase'),
'actions':orm.relation(KeyphraseAction),
})

orm.mapper(Action, action_table)

orm.mapper(KeyphraseAction, keyphrase_action_table, properties={
'action':orm.relation(Action),
'successMessage':orm.relation(Message, primaryjoin =
keyphrase_action_table.c.success_message_id == message_table.c.id),
'failureMessage':orm.relation(Message, primaryjoin =
keyphrase_action_table.c.failure_message_id == message_table.c.id),
'emailContent':orm.relation(EmailContent, primaryjoin =
keyphrase_action_table.c.email_content_id ==
email_content_table.c.id),
})

When I attempt to delete a KeyphraseAction from a Keyphrase instance
as follows:

# kp is a Keyphrase instance
for i, ka in enumerate(kp.actions):
del kp.actions[i]# this doesn't work!

It throws an error:

AssertionError: Dependency rule tried to blank-out primary key
column 'keyphrase_action.keyphrase_id' on instance

But this does work:

assoc = meta.Session.query(m.KeyphraseAction)\
.filter(and_(m.KeyphraseAction.keyphrase_id == kp.id,
m.KeyphraseAction.action_id == kp.actions[i].action.id))\
.one()

meta.Session.delete(assoc)

What am I doing wrong?


On Aug 5, 3:05 pm, Hollister a.hollister.willi...@gmail.com wrote:
 That was exactly the conclusion I reached before I read your reply. I
 modeled it that way and it seems to work perfectly. Guess I was just
 overthinking it.

 Thanks for getting back to me, Mike.

 On Aug 3, 11:41 pm, Michael Bayer mike...@zzzcomputing.com wrote:

  On Aug 3, 2009, at 5:21 PM, Hollister wrote:

   I have 2 tables which are related to each other through an M:N
   relationship (Keyword  Action). Additionally, the relationship itself
   has attributes, which I have as non-key attributes in a third table
   (KeywordAction). I've modeled this dozens of different ways, but have
   yet to get exactly what I want from the model.

   At the ORM level, I want Keyword to have a property that is a
   collection of KeywordAction instances. Each KeywordAction instance
   would have a single Action instance property, so I could do things
   like this:

   ---
   for ka in keyword.keyword_actions:
      if ka.status == 'open':
          ka.action.do_something()

   keyword.keyword_actions.append(KeywordAction(action, status = 'open'))
   ---

   I've tried using the association_proxy, but I get the feeling that's
   not the right tool for this job.

  the above example doesn't seem like it would need association proxy, a  
  simple collection of relation()s, i.e. Keyword.keyword_actions,  
  KeywordAction.action would do based on the navigation illustrated.


--~--~-~--~~~---~--~~
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: Advice on modeling a many-to-many relationship

2009-08-06 Thread Hollister

Also, the following does nothing at all (does not delete and throws no
errors):

for ka in kp.actions:
del ka

But this:

del kp.actions

Throws the same assertion error.


On Aug 6, 11:01 am, Hollister a.hollister.willi...@gmail.com wrote:
 Still having a little trouble...here are the relevant mappings:

     orm.mapper(Keyphrase, keyphrase_table, properties = {
         'message':orm.relation(Message, backref='keyphrase'),
         'campaign':orm.relation(Campaign, backref='keyphrase'),
         'actions':orm.relation(KeyphraseAction),
     })

     orm.mapper(Action, action_table)

     orm.mapper(KeyphraseAction, keyphrase_action_table, properties={
         'action':orm.relation(Action),
         'successMessage':orm.relation(Message, primaryjoin =
 keyphrase_action_table.c.success_message_id == message_table.c.id),
         'failureMessage':orm.relation(Message, primaryjoin =
 keyphrase_action_table.c.failure_message_id == message_table.c.id),
         'emailContent':orm.relation(EmailContent, primaryjoin =
 keyphrase_action_table.c.email_content_id ==
 email_content_table.c.id),
     })

 When I attempt to delete a KeyphraseAction from a Keyphrase instance
 as follows:

     # kp is a Keyphrase instance
     for i, ka in enumerate(kp.actions):
         del kp.actions[i]    # this doesn't work!

 It throws an error:

     AssertionError: Dependency rule tried to blank-out primary key
 column 'keyphrase_action.keyphrase_id' on instance

 But this does work:

     assoc = meta.Session.query(m.KeyphraseAction)\
         .filter(and_(m.KeyphraseAction.keyphrase_id == kp.id,
 m.KeyphraseAction.action_id == kp.actions[i].action.id))\
         .one()

     meta.Session.delete(assoc)

 What am I doing wrong?

 On Aug 5, 3:05 pm, Hollister a.hollister.willi...@gmail.com wrote:

  That was exactly the conclusion I reached before I read your reply. I
  modeled it that way and it seems to work perfectly. Guess I was just
  overthinking it.

  Thanks for getting back to me, Mike.

  On Aug 3, 11:41 pm, Michael Bayer mike...@zzzcomputing.com wrote:

   On Aug 3, 2009, at 5:21 PM, Hollister wrote:

I have 2 tables which are related to each other through an M:N
relationship (Keyword  Action). Additionally, the relationship itself
has attributes, which I have as non-key attributes in a third table
(KeywordAction). I've modeled this dozens of different ways, but have
yet to get exactly what I want from the model.

At the ORM level, I want Keyword to have a property that is a
collection of KeywordAction instances. Each KeywordAction instance
would have a single Action instance property, so I could do things
like this:

---
for ka in keyword.keyword_actions:
   if ka.status == 'open':
       ka.action.do_something()

keyword.keyword_actions.append(KeywordAction(action, status = 'open'))
---

I've tried using the association_proxy, but I get the feeling that's
not the right tool for this job.

   the above example doesn't seem like it would need association proxy, a  
   simple collection of relation()s, i.e. Keyword.keyword_actions,  
   KeywordAction.action would do based on the navigation illustrated.


--~--~-~--~~~---~--~~
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: Advice on modeling a many-to-many relationship

2009-08-05 Thread Hollister

That was exactly the conclusion I reached before I read your reply. I
modeled it that way and it seems to work perfectly. Guess I was just
overthinking it.

Thanks for getting back to me, Mike.

On Aug 3, 11:41 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 On Aug 3, 2009, at 5:21 PM, Hollister wrote:





  I have 2 tables which are related to each other through an M:N
  relationship (Keyword  Action). Additionally, the relationship itself
  has attributes, which I have as non-key attributes in a third table
  (KeywordAction). I've modeled this dozens of different ways, but have
  yet to get exactly what I want from the model.

  At the ORM level, I want Keyword to have a property that is a
  collection of KeywordAction instances. Each KeywordAction instance
  would have a single Action instance property, so I could do things
  like this:

  ---
  for ka in keyword.keyword_actions:
     if ka.status == 'open':
         ka.action.do_something()

  keyword.keyword_actions.append(KeywordAction(action, status = 'open'))
  ---

  I've tried using the association_proxy, but I get the feeling that's
  not the right tool for this job.

 the above example doesn't seem like it would need association proxy, a  
 simple collection of relation()s, i.e. Keyword.keyword_actions,  
 KeywordAction.action would do based on the navigation illustrated.
--~--~-~--~~~---~--~~
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] Advice on modeling a many-to-many relationship

2009-08-03 Thread Hollister

I have 2 tables which are related to each other through an M:N
relationship (Keyword  Action). Additionally, the relationship itself
has attributes, which I have as non-key attributes in a third table
(KeywordAction). I've modeled this dozens of different ways, but have
yet to get exactly what I want from the model.

At the ORM level, I want Keyword to have a property that is a
collection of KeywordAction instances. Each KeywordAction instance
would have a single Action instance property, so I could do things
like this:

---
for ka in keyword.keyword_actions:
if ka.status == 'open':
ka.action.do_something()

keyword.keyword_actions.append(KeywordAction(action, status = 'open'))
---

I've tried using the association_proxy, but I get the feeling that's
not the right tool for this job.

 Any advice would be appreciated!
--~--~-~--~~~---~--~~
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 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 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: 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

[sqlalchemy] Re: deletes using association_proxy

2009-06-03 Thread Hollister

Mike, thanks for the quick reply.
I suspected it was a cascade issue, and have been through the docs and
tried various configs. At the risk of appearing stupid, can you point
me in the right direction? Do the cascades only need to be on the
association table, or also on the left  right parent tables? If a
parent (Keyphrase or Action) is deleted, then I want the delete to
cascade to the association (KeyphraseAction), but not vice versa.

On Jun 3, 10:26 am, Michael Bayer mike...@zzzcomputing.com wrote:
 hollister wrote:

  # mappers
  mapper(Keyphrase, keyphrase_table)
  mapper(Action, action_table)

  mapper(KeyphraseAction, keyphrase_action_table, properties={
          'keyphrase': relation(Keyphrase,
              backref = 'keyphrase_action'),
          'action': relation(Action),
      })

  # test
  for i, action in enumerate(kp.actions):
      print action.action_name
      kp.actions.remove(action)   # this fails!

  s.commit()

 you need to configure cascade so that SQLA knows to delete a
 KeyphraseAction when it is deassociated from a Keyphrase.  See the mapping
 docs for information on delete cascade.
--~--~-~--~~~---~--~~
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] deletes using association_proxy

2009-06-02 Thread hollister

I have a many-to-many schema using an association object and the
association proxy. I'm able to add data via the ORM, but trying to
delete from the association (but not delete the left or right tables)
throws a AssertionError: Dependency rule tried to blank-out primary
key column 'keyphrase_action.keyphrase_id' on instance
'KeyphraseAction at 0x9da7d8c' error. My test code is below. Any
advice is appreciated!

import os, sys

from sqlalchemy import Column, Integer, String, Table, create_engine,
schema, types
from sqlalchemy import orm, MetaData, Column, ForeignKey
from sqlalchemy.orm import relation, mapper, sessionmaker
from sqlalchemy.ext.associationproxy import association_proxy

engine = create_engine('sqlite:home/aw/desktop/test.db',
echo=True)

meta = MetaData(bind=engine)

# schema
keyphrase_table = schema.Table('keyphrase', meta.metadata,
schema.Column('id', types.Integer, primary_key = True,
autoincrement = True),
schema.Column('phrase', types.String(160), nullable = False),
)

action_table = schema.Table('action', meta.metadata,
schema.Column('id', types.Integer, primary_key = True,
autoincrement = True),
schema.Column('action_name', types.Text, nullable = False),
)

keyphrase_action_table = schema.Table('keyphrase_action',
meta.metadata,
schema.Column('keyphrase_id', types.Integer, schema.ForeignKey
('keyphrase.id'), primary_key = True),
schema.Column('action_id', types.Integer, schema.ForeignKey
('action.id'), primary_key = True),
schema.Column('is_deferred', types.Boolean, nullable = False),
)

meta.create_all()


# classes
class Keyphrase(object):
def __init__(self, phrase):
self.phrase = phrase

# creator function
def _getKeyphraseAction(d):
return KeyphraseAction(action = d['action'], isDeferred = d
['isDeferred'])

actions = association_proxy('keyphrase_action', 'action', creator
= _getKeyphraseAction)

class Action(object):
def __init__(self, name):
self.action_name = name

class KeyphraseAction(object):
def __init__(self, keyphrase = None, action = None, isDeferred =
False):
self.keyphrase = keyphrase
self.action = action
self.is_deferred = isDeferred

# mappers
mapper(Keyphrase, keyphrase_table)
mapper(Action, action_table)

mapper(KeyphraseAction, keyphrase_action_table, properties={
'keyphrase': relation(Keyphrase,
backref = 'keyphrase_action'),
'action': relation(Action),
})

# test code
Session = sessionmaker(bind=engine)
s = Session()

# add some data
kp = Keyphrase('fast')
a = Action('capture_email')

s.add(kp)
s.add(a)
s.commit()

# assciate the keyphrase to the action
kp.actions.append({'action':a,'isDeferred':True})
s.commit()

#remove the newly created association, leaving the keyphrase and
actions
kp = s.query(Keyphrase).get(kp.id)

for i, action in enumerate(kp.actions):
print action.action_name
kp.actions.remove(action)   # this fails!

s.commit()


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---