Re: [sqlalchemy] .one is not returning a KeyedTuple, but a

2017-04-24 Thread mike bayer



On 04/24/2017 12:44 PM, Mark Jones wrote:
We are upgrading from 0.9.7 to 1.1.9 and I've run into a bit of a 
problem with the upgrade.


I have a query which  is based on a .select_from() and .join() so the 
mapping doesn't really exist from a single model.  All of the fields 
have labels though and the result set produces the right results.  The 
problem comes from the return type of the results:


def one_or_none(self):
 ret = list(self)

 l = len(ret)
 if l == 1:
 return ret[0]
 elif l == 0:
 return None
 else:
 raise orm_exc.MultipleResultsFound("Multiple rows were 
found for one_or_none()")
When it returns ret[0] is not returning an exported sqlalchemy type. 
  Before it would return a KeyedTuple or based on some of our code maybe 
even a RowProxywhich we would use to control serialization to json for 
the response to the client.  However, this new type is NOT a KeyTupleand 
therefore isinstance(obj, KeyedTuple) returns False and then our code 
fails. isinstance(obj, AbstractKeyedTuple)does return True, but that 
isn't one of the exported classes from sqlalchemy.util. isinstance(obj, 
_LW) returns True as well, but the same objections affect that type as well


The actual class looks like:

 >>> obj.__class__
 

I could fix this by simply importing it from _collections, but that 
doesn't seem like the right solution.  So, I'm wondering if this is a 
bug or if I'm just looking at the problem wrong.  I could pick up on the 
presence of the keys() method and use that, but what if that conflicts 
with a field in the db.


why don't you use isinstance(result, tuple) or even isinstance(result, 
collections.Sequence) ?   That would be the usual Python technique  to use.







A shortened version of the query looks like this:
 return db.query(Account.name.label('account_name'),
 Account.id.label('id'),
 Account.id.label('account_id'),
 
select([func.coalesce(func.sum(...).as_scalar().label('count'),

 not_(Account.disabled).label('enabled'),
 AccountStatus.name.label('account_status'),
 User.login,
 User.first_name,
 User.last_name,
  .select_from(Account)\
  .join(User, User.id == Account.primary_user_id)\
  .join(AccountStatus, Account.account_status_id == 
AccountStatus.id)\
  .outerjoin(account_manager, 
Account.account_manager_user_id == account_manager.id)


--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and 
Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
description.

---
You received this message because you are subscribed to the Google 
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to sqlalchemy+unsubscr...@googlegroups.com 
.
To post to this group, send email to sqlalchemy@googlegroups.com 
.

Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


--
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper


http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.

To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] .one is not returning a KeyedTuple, but a

2017-04-24 Thread Mark Jones
We are upgrading from 0.9.7 to 1.1.9 and I've run into a bit of a problem 
with the upgrade.

I have a query which  is based on a .select_from() and .join() so the 
mapping doesn't really exist from a single model.  All of the fields have 
labels though and the result set produces the right results.  The problem 
comes from the return type of the results:

def one_or_none(self):
ret = list(self)

l = len(ret)
if l == 1:
return ret[0]
elif l == 0:
return None
else:
raise orm_exc.MultipleResultsFound("Multiple rows were found 
for one_or_none()")
 
When it returns ret[0] is not returning an exported sqlalchemy type. 
 Before it would return a KeyedTuple or based on some of our code maybe 
even a RowProxy which we would use to control serialization to json for the 
response to the client.  However, this new type is NOT a KeyTuple and 
therefore isinstance(obj, KeyedTuple) returns False and then our code 
fails.  isinstance(obj, AbstractKeyedTuple) does return True, but that 
isn't one of the exported classes from sqlalchemy.util.  isinstance(obj, 
_LW) returns True as well, but the same objections affect that type as well

The actual class looks like:

>>> obj.__class__


I could fix this by simply importing it from _collections, but that doesn't 
seem like the right solution.  So, I'm wondering if this is a bug or if I'm 
just looking at the problem wrong.  I could pick up on the presence of the 
keys() method and use that, but what if that conflicts with a field in the 
db.

A shortened version of the query looks like this:
return db.query(Account.name.label('account_name'),
Account.id.label('id'),
Account.id.label('account_id'),

select([func.coalesce(func.sum(...).as_scalar().label('count'),
not_(Account.disabled).label('enabled'),
AccountStatus.name.label('account_status'),
User.login,
User.first_name,
User.last_name,
 .select_from(Account)\
 .join(User, User.id == Account.primary_user_id)\
 .join(AccountStatus, Account.account_status_id == 
AccountStatus.id)\
 .outerjoin(account_manager, 
Account.account_manager_user_id == account_manager.id)

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.