Re: [sqlalchemy] sqlalchemy.exc.DataError: (psycopg2.DataError) integer out of range

2015-07-14 Thread Chengjun JIN
Hi Michael,

Thanks for your time.

I have figured out the problem. My error is not related to the data type, 
as you pointed out. It is related to the definition of the TABLE column 
repeatedly and inconsistently (meaning: initially I define the column as 
Integer in the database, then I changed to Numeric but it is not changed, 
due to my bad coding).

Regards,
Chengjun



On Tuesday, July 14, 2015 at 1:47:08 AM UTC+2, Michael Bayer wrote:

  

 On 7/13/15 7:02 PM, Chengjun JIN wrote:
  
 Hi all, 

  I am trying to save data into postgresql via sqlchemy ORM. I encountered 
 an error below: sqlalchemy.exc.DataError: (psycopg2.DataError) integer 
 out of range

  I pinpoint the place where it goes wrong. I have a large number which is 
 2468432255.0. 
 If I change to smaller number like 468432255.0, then it works.

  The thing confused me is that: I define the column as volume = 
 Column(Numeric). As far as I understand, Numeric should be able to handle 
 this large number.  Additionally, I tried other data type like BigInt 
 etc... They all gave me the same error.
  

 I can't reproduce that.  Here's a test:

 from sqlalchemy import create_engine, Numeric, MetaData, Table, Column

 m = MetaData()
 t = Table('x', m, Column('data', Numeric))
 e = create_engine(postgresql://scott:tiger@localhost/test, echo=True)
 c = e.connect()
 tr = c.begin()
 m.create_all(c)

 c.execute(
 t.insert(),
 {data: 2468432255.0}
 )



 What is the actual NUMERIC type in the schema?  If you make a NUMERIC type 
 with too low of a precision/scale, the number will be rejected.  example:

 from sqlalchemy import create_engine, Numeric, MetaData, Table, Column

 m = MetaData()
 t = Table('x', m, Column('data', Numeric(6, 2)))
 e = create_engine(postgresql://scott:tiger@localhost/test, echo=True)
 c = e.connect()
 tr = c.begin()
 m.create_all(c)

 c.execute(
 t.insert(),
 {data: 2468432255.0}
 )


 output:

 sqlalchemy.exc.DataError: (psycopg2.DataError) numeric field overflow
 DETAIL:  A field with precision 6, scale 2 must round to an absolute value 
 less than 10^4.
  [SQL: 'INSERT INTO x (data) VALUES (%(data)s)'] [parameters: {'data': 
 2468432255.0}]



 Please adapt the test above to illustrate how you're getting your error 
 exactly.  




  
  Any idea?

  Thanks,
 Chengjun

  
  -- 
 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+...@googlegroups.com javascript:.
 To post to this group, send email to sqlal...@googlegroups.com 
 javascript:.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.


  

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Re: How to jsonify Query result?

2015-07-14 Thread sector119


I create Serializer class and use it for all my models with: Base = 
declarative_base(cls=Serializer)


But I have one problem! I have Organization model and Service model that has 
organization = relationship('Organization', backref='services') relationship 
and I get RuntimeError: maximum recursion depth exceeded while calling a 
Python object


What can I do with that? Help me please!


Traceback (most recent call last):
  File s.py, line 10, in module
print repr(r.to_dict())
  File /Users/sector119/PycharmProjects/epsilon/epsilon/models/serializer.py, 
line 31, in to_dict
value = self._serialize(key, field.value)
  File /Users/sector119/PycharmProjects/epsilon/epsilon/models/serializer.py, 
line 48, in _serialize
result.append(cls._serialize(key, v))
  File /Users/sector119/PycharmProjects/epsilon/epsilon/models/serializer.py, 
line 51, in _serialize
result = value.to_dict()
  File /Users/sector119/PycharmProjects/epsilon/epsilon/models/serializer.py, 
line 31, in to_dict

...
...
...
  File 
/Users/sector119/PythonVirtualEnv/epsilon/lib/python2.7/site-packages/sqlalchemy/orm/state.py,
 
line 671, in value
self.state.obj(), self.state.class_)
  File 
/Users/sector119/PythonVirtualEnv/epsilon/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py,
 
line 233, in __get__
dict_ = instance_dict(instance)
RuntimeError: maximum recursion depth exceeded while calling a Python object



from datetime import datetime, date

from sqlalchemy.inspection import inspect


class Serializer(object):
__public__ = None
__private__ = None

def to_dict(self, exclude=(), extra=()):
data = {}
items = inspect(self).attrs.items()

if self.__public__:
public = self.__public__ + extra
else:
public = extra

if self.__private__:
private = self.__private__ + exclude
else:
private = exclude

for key, field in items:
if private and key in private:
continue

if public and key not in public:
continue

value = self._serialize(key, field.value)
if value:
data[key] = value

return data

@classmethod
def _serialize(cls, key, value):
print 'Field name: %s' % key

if isinstance(value, (datetime, date)):
print 'Type date'
result = value.isoformat()
elif hasattr(value, '__iter__'):
print 'Type iterable'
result = []
for v in value:
result.append(cls._serialize(key, v))
elif isinstance(value, Serializer):
print 'Type Serializer'
result = value.to_dict()
else:
print 'Type other'
result = value

print 'Result %r\n' % result
return result




-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.