[Sqlalchemy-commits] [5264] sqlalchemy/trunk: - Query.count() and Query.get() return a more informa
Brought to you by:
zzzeek
From: <co...@sq...> - 2008-11-08 21:18:17
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!-- #msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; } #msg dt { float: left; width: 6em; font-weight: bold; } #msg dt:after { content:':';} #msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; } #msg dl a { font-weight: bold} #msg dl a:link { color:#fc3; } #msg dl a:active { color:#ff0; } #msg dl a:visited { color:#cc6; } h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; } #msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; } #msg ul, pre { overflow: auto; } #header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; } #patch { width: 100%; } #patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;} #patch .propset h4, #patch .binary h4 {margin:0;} #patch pre {padding:0;line-height:1.2em;margin:0;} #patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;} #patch .propset .diff, #patch .binary .diff {padding:10px 0;} #patch span {display:block;padding:0 10px;} #patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;} #patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;} #patch .lines, .info {color:#888;background:#fff;} --></style> <title>[5264] sqlalchemy/trunk: - Query.count() and Query.get() return a more informative</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>5264</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2008-11-08 16:18:11 -0500 (Sat, 08 Nov 2008)</dd> </dl> <h3>Log Message</h3> <pre>- Query.count() and Query.get() return a more informative error message when executed against multiple entities. [ticket:1220]</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemytrunkCHANGES">sqlalchemy/trunk/CHANGES</a></li> <li><a href="#sqlalchemytrunkVERSION">sqlalchemy/trunk/VERSION</a></li> <li><a href="#sqlalchemytrunklibsqlalchemyormquerypy">sqlalchemy/trunk/lib/sqlalchemy/orm/query.py</a></li> <li><a href="#sqlalchemytrunktestormquerypy">sqlalchemy/trunk/test/orm/query.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemytrunkCHANGES"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/CHANGES (5263 => 5264)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/CHANGES 2008-11-08 21:00:15 UTC (rev 5263) +++ sqlalchemy/trunk/CHANGES 2008-11-08 21:18:11 UTC (rev 5264) </span><span class="lines">@@ -6,6 +6,12 @@ </span><span class="cx"> ======= </span><span class="cx"> 0.5.0rc4 </span><span class="cx"> ======== </span><ins>+- bugfixes and behavioral changes +- orm + - Query.count() and Query.get() return a more informative + error message when executed against multiple entities. + [ticket:1220] + </ins><span class="cx"> - mssql </span><span class="cx"> - Lots of cleanup and fixes to correct problems with </span><span class="cx"> limit and offset. </span></span></pre></div> <a id="sqlalchemytrunkVERSION"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/VERSION (5263 => 5264)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/VERSION 2008-11-08 21:00:15 UTC (rev 5263) +++ sqlalchemy/trunk/VERSION 2008-11-08 21:18:11 UTC (rev 5264) </span><span class="lines">@@ -1 +1 @@ </span><del>-0.5.0rc3 </del><ins>+0.5.0rc4 </ins></span></pre></div> <a id="sqlalchemytrunklibsqlalchemyormquerypy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/orm/query.py (5263 => 5264)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/orm/query.py 2008-11-08 21:00:15 UTC (rev 5263) +++ sqlalchemy/trunk/lib/sqlalchemy/orm/query.py 2008-11-08 21:18:11 UTC (rev 5264) </span><span class="lines">@@ -236,14 +236,14 @@ </span><span class="cx"> return None </span><span class="cx"> return self._entities[0].mapper </span><span class="cx"> </span><del>- def _only_mapper_zero(self): </del><ins>+ def _only_mapper_zero(self, rationale=None): </ins><span class="cx"> if len(self._entities) > 1: </span><del>- raise sa_exc.InvalidRequestError("This operation requires a Query against a single mapper.") </del><ins>+ raise sa_exc.InvalidRequestError(rationale or "This operation requires a Query against a single mapper.") </ins><span class="cx"> return self._mapper_zero() </span><span class="cx"> </span><del>- def _only_entity_zero(self): </del><ins>+ def _only_entity_zero(self, rationale=None): </ins><span class="cx"> if len(self._entities) > 1: </span><del>- raise sa_exc.InvalidRequestError("This operation requires a Query against a single mapper.") </del><ins>+ raise sa_exc.InvalidRequestError(rationale or "This operation requires a Query against a single mapper.") </ins><span class="cx"> return self._entity_zero() </span><span class="cx"> </span><span class="cx"> def _generate_mapper_zero(self): </span><span class="lines">@@ -410,7 +410,7 @@ </span><span class="cx"> if hasattr(ident, '__composite_values__'): </span><span class="cx"> ident = ident.__composite_values__() </span><span class="cx"> </span><del>- key = self._only_mapper_zero().identity_key_from_primary_key(ident) </del><ins>+ key = self._only_mapper_zero("get() can only be used against a single mapped class.").identity_key_from_primary_key(ident) </ins><span class="cx"> return self._get(key, ident) </span><span class="cx"> </span><span class="cx"> @classmethod </span><span class="lines">@@ -1248,7 +1248,12 @@ </span><span class="cx"> def count(self): </span><span class="cx"> """Apply this query's criterion to a SELECT COUNT statement.""" </span><span class="cx"> </span><del>- return self._col_aggregate(sql.literal_column('1'), sql.func.count, nested_cols=list(self._only_mapper_zero().primary_key)) </del><ins>+ return self._col_aggregate(sql.literal_column('1'), sql.func.count, + nested_cols=list(self._only_mapper_zero( + "Can't issue count() for multiple types of objects or columns. " + " Construct the Query against a single element as the thing to be counted, " + "or for an actual row count use Query(func.count(somecolumn)) or " + "query.values(func.count(somecolumn)) instead.").primary_key)) </ins><span class="cx"> </span><span class="cx"> def _col_aggregate(self, col, func, nested_cols=None): </span><span class="cx"> context = QueryContext(self) </span></span></pre></div> <a id="sqlalchemytrunktestormquerypy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/test/orm/query.py (5263 => 5264)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/test/orm/query.py 2008-11-08 21:00:15 UTC (rev 5263) +++ sqlalchemy/trunk/test/orm/query.py 2008-11-08 21:18:11 UTC (rev 5264) </span><span class="lines">@@ -236,6 +236,14 @@ </span><span class="cx"> # this is fine, however </span><span class="cx"> q.from_self() </span><span class="cx"> </span><ins>+ def test_mapper_zero(self): + s = create_session() + + q = s.query(User, Address) + self.assertRaises(sa_exc.InvalidRequestError, q.count) + + self.assertRaises(sa_exc.InvalidRequestError, q.get, 5) + </ins><span class="cx"> def test_from_statement(self): </span><span class="cx"> s = create_session() </span><span class="cx"> </span></span></pre> </div> </div> </body> </html> |